【问题标题】:Selenium Python Element not found未找到 Selenium Python 元素
【发布时间】:2020-09-08 11:19:17
【问题描述】:

这是我要抓取的 HTML 链接 https://pk.khaadi.com/unstitched/r20206-red-r20206-red-pk.html

<div class="swatch-attribute-options clearfix">
      <div class="swatch-option color selected" option-type="1" option- 
       id="61" option-label="RED" option-tooltip-thumb="" option-tooltip- 
       value="#ee0000" "="" style="background: #ee0000 no-repeat center; 
       background-size: initial;">
      </div>
      <div class="swatch-option color selected" option-type="1" option- 
       id="73" option-label="YELLOW" option-tooltip-thumb="" option-tooltip- 
       value="#feed00" "="" style="background: #feed00 no-repeat center; 
       background-size: initial;">
      </div>
</div>
Color = S_Driver.find_elements_by_xpath( '//*[@id="product-options-wrapper"]/div/div/div[1]/div' )

Xpath 是两个颜色 div 都存在的外部 div

            for c in Color:
            n_Color.append(c.get_attribute( 'option-label' ))
            print( n_Color + '\n' )

这就是我尝试通过“选项标签”属性提取颜色的方式

【问题讨论】:

  • 请阅读为什么是screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
  • 另外,可以分享一下网页的网址吗?
  • 完成你建议的更改@DebanjanB

标签: python selenium web-scraping attributes element


【解决方案1】:

使用以下命令更改 xpath:

//div[@class='swatch-option color']

根据提供的截图创建,希望页面上没有其他匹配基于此的。如果是这样,请将其更改为:

//div[@class='swatch-option color' and @option-type='1']

【讨论】:

  • 它仍然返回空列表
  • 我认为我要废弃的数据是动态的,因为它不存在于查看页面源中
  • 作为前端元素或 xpath 的数据不再有效?
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 2021-12-17
  • 2018-11-07
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多