【问题标题】:How to click on a link with find_element_by_* through Selenium and python如何通过 Selenium 和 python 点击​​带有 find_element_by_* 的链接
【发布时间】:2019-05-23 00:33:20
【问题描述】:

我需要点击收件箱中的确认邮件。但无法点击链接。

这是我的html和尝试过的代码

<td bgcolor="#ffffff" style="padding:20px;">
<div style="color:rgb(0,0,0);font-size:16px;">
<strong>You are almost done!</strong><br><br>
    To complete Sunday Lankadeepa E-Paper registration, please click the link below:
</div>
<br><br>
<div style="width:616px;">
<a href="http://Sundaylankadeepa.newspaperdirect.com/epaper/confirmmail.aspx?code=ZUZ3S2K17GR&amp;rt=trial" rel="nofollow">

http://Sundaylankadeepa.newspaperdirect.com/epaper/confirmmail.aspx?code=ZUZ3K17GR&amp;rt=trial</a>
</div>
<br><br>
If clicking the link doesn't work, please select and copy the entire link. Then open a browser, paste the link in the address bar, and press Enter or Return on your keyboard.

尝试过的代码...

text = "newspaperdirect"
element = driver.find_element_by_xpath('//a[contains(text(),"%s")]' % text)
element.click()

错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: 
{"method":"xpath","selector":"//a[contains(text(),"newspaperdirect")]"}
 (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),
platform=Windows NT 6.2.9200 x86_64)

【问题讨论】:

  • 尝试wait for link然后点击
  • 可能是您的 xpath 路径为您提供了 Web 元素列表。所以使用 driver.find_elements_by_xpath('//a[contains(text(),"%s")]' % text) 将其存储在 weblist 中并对其进行迭代

标签: python selenium xpath css-selectors webdriver


【解决方案1】:

添加一些等待并使用find_element_by_partial_link_text

import time
from selenium import webdriver
browser = webdriver.Chrome(executable_path='/home/bitto/chromedriver')
url='file:///test.html' # url or .html file path here
text = "newspaperdirect"
browser.implicitly_wait(20) # seconds
browser.get(url)
browser.find_element_by_partial_link_text(text).click()

【讨论】:

  • 不应将sleep() 与硒一起使用,因为@Anderson 已经评论过您应该将WaitsWebDriverWait 一起使用,如下所示... selenium-python.readthedocs.io/waits.html#waits
  • @MosheSlavin 感谢您指出这一点!我一定会看看这个并更新我的答案。
  • @CoreyGoldberg 对不起。我正在做其他事情,然后将其复制粘贴到下面的编辑器中。编辑了它。
  • 隐式等待只比 time.sleep 稍微好一点。这通常不是您想要做的。使用 WebdriverWait
【解决方案2】:

文本/单词newspaperdirect&lt;a&gt;标签的href属性的一部分,也是innerHTML的一部分

解决方案

要点击元素,您可以使用以下任一解决方案:

  • 使用PARTIAL_LINK_TEXT

    driver.find_element_by_partial_link_text("newspaperdirect").click()
    
  • 使用CSS_SELECTOR

    driver.find_element_by_css_selector("div>a[rel='nofollow'][href*='newspaperdirect']").click()
    
  • 使用XPATH

    driver.find_element_by_xpath("//div/a[@rel='nofollow' and contains(@href, 'newspaperdirect')]").click()
    

【讨论】:

    【解决方案3】:

    您可以使用显式等待

    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((By.XPATH, '//a[contains(@rel,'nofollow') and contains(@href, 'newspaperdirect')]')))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 2020-11-27
      • 2022-08-11
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多