Skip to main content
 首页 » 编程设计

python之无法启动有问题的链接的点击

2024年09月07日21JustinYoung

我已经用 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。那就是我想点击的地方。当您将鼠标悬停在该链接上时,会注意到链接上有一个阴影。希望很清楚我想做什么。提前致谢。

请您参考如下方法:

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

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