【问题标题】:Can't initiate a click on a problematic link无法点击有问题的链接
【发布时间】:2017-12-25 08:27:34
【问题描述】:

我在 python 中结合 selenium 编写了一个脚本来启动对网页中某个链接的点击。我唯一的目的是点击那个链接。我尝试了几种不同的方法,但无法正常工作。

网页链接:URL

我尝试过的脚本:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("use_above_url")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".k-selectable"))).click()
driver.quit()

可点击链接所在的元素:

  <div class="k-grid-content k-auto-scrollable">
   <table class="k-selectable" data-role="selectable" role="grid" style="touch-action: none;">
    <colgroup>
     <col style="width:100px"/>
     <col style="width:210px"/>
     <col/>
     <col/>
     <col style="width:120px"/>
    </colgroup>
    <tbody role="rowgroup">
     <tr class="rowHover" data-uid="1fccd732-cd65-4393-b1be-66786fe9ee60" role="row">
      <td role="gridcell">
       R016698
      </td>
      <td role="gridcell">
       R-13-0410-0620-50000
      </td>
      <td role="gridcell" style="display:none">
       O0485204
      </td>
      <td role="gridcell">
       GOOCH, PHILIP L
      </td>
      <td role="gridcell">
       319 LIZZIE ST, TAYLOR, TX  76574
      </td>
      <td role="gridcell" style="display:none">
       DOAK ADDITION, BLOCK 62, LOT 5
      </td>
      <td role="gridcell" style="display:none">
       T541
      </td>
      <td role="gridcell" style="display:none">
      </td>
      <td role="gridcell" style="display:none">
       S3564 - Doak Addition
      </td>
      <td role="gridcell" style="display:none">
       Real
      </td>
      <td role="gridcell">
       <div style="text-align:right;width:100%">
        $46,785
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>

如果您按照上面的 url,那么您可以在该网页中看到包含此​​确切文本 R016698 R-13-0410-0620-50000 GOOCH, PHILIP L 319 LIZZIE ST, TAYLOR, TX 76574 的一行。那就是我想点击的地方。当您将鼠标悬停在该链接上时,会注意到链接上出现阴影。希望很清楚我想做什么。提前致谢。

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver web-scraping


    【解决方案1】:

    页面上有两个类"k-selectable" 的元素。第一个是隐藏的,所以等待EC.visibility_of_element_located 总是会失败……你需要处理第二个。所以只需应用更具体的选择器:

    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".k-selectable tr[role='row']"))).click()
    

    【讨论】:

    • 如何知道还有一个同名的类?很好奇先生。再次感谢。
    • @class 名称不是唯一标识符。它用于将特定样式应用于 HTML 元素,并且可能有很多具有相同类名的元素......您可以使用print(len(driver.find_elements(your_selector))) 进行调试。如果你在输出中得到超过1,那么你应该应用更具体的选择器
    猜你喜欢
    • 2022-07-30
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2012-04-19
    相关资源
    最近更新 更多