简体   繁体   English

单击 Selenium 中的链接:find_element_by_link_text 不起作用

[英]Click on a link in Selenium: find_element_by_link_text doesn't work

I am going to click on a link via selenium but it doesn't work when I applied find_element_by_link_text as a following: (URL: https://healthunlocked.com/positivewellbeing )我将通过 selenium 单击一个链接,但是当我按以下方式应用 find_element_by_link_text 时它不起作用:(URL: https://healthunlocked.com/positivewellbeing

HTML Code HTML 代码

<a class href="/positivewellbeing/posts">Posts</a>

Selenium Code: Selenium 代码:

driver.find_element_by_link_text('Posts').click()

I don't have any error, but this code line didn't run or work我没有任何错误,但此代码行没有运行或工作

Try to add .element_to_be_clickable before clicking:尝试在点击之前添加.element_to_be_clickable

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Posts'))).click()

#update here
wait.until(EC.url_to_be('https://healthunlocked.com/positivewellbeing/posts'))
print(driver.current_url)

The following imports:以下进口:

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

Based on your comments, if you want to wait for the url to change to the way you want, please use EC.url_to_be .根据您的评论,如果您想等待 url 更改为您想要的方式,请使用EC.url_to_be Because time.sleep (...) is a bad way, please see the above updated code.因为time.sleep (...)是一个不好的方法,请看上面更新的代码。

See more about expected_conditions here:在此处查看有关expected_conditions的更多信息:

https://selenium-python.readthedocs.io/api.html#expected-conditions-support https://selenium-python.readthedocs.io/api.html#expected-conditions-support

If you are using.Net you can try to use Coypu library in your project:如果您使用的是.Net,您可以尝试在您的项目中使用 Coypu 库:

https://github.com/featurist/coypu https://www.nuget.org/packages/Coypu/ https://github.com/featurist/coypu https://www.nuget.org/packages/Coypu/

Coypu it's a wrapper that will save you from retries, sleeps (waits). Coypu 它是一个包装器,可以让你免于重试、休眠(等待)。

It supports browser automation in.Net to help make tests readable, robust, fast to write and less tied to a UI.它支持 .Net 中的浏览器自动化,以帮助使测试具有可读性、健壮性、编写速度快且与 UI 的联系更少。 If your tests are littered with sleeps, retries, complex XPath expressions and IDs then Coypu might help.如果您的测试充满了睡眠、重试、复杂的 XPath 表达式和 ID,那么 Coypu 可能会有所帮助。

Coypu is on Nuget: Coypu 在 Nuget 上:

PM> Install-Package Coypu

I work in a project where we have around 3k automated tests developed with C# and Selenium and my life would be kind of miserable without Coypu.我在一个项目中工作,我们使用 C# 和 Selenium 开发了大约 3k 个自动化测试,如果没有 Coypu,我的生活会有点悲惨。

Your code will look the same when using Coypu but the sleep will be automatically handled by the library, it's transparent to the user:使用 Coypu 时,您的代码看起来相同,但睡眠将由库自动处理,它对用户是透明的:

Browser.FindXPath("//*[contains(text(), 'Posts')]").Click();

Browser.FindIdEndingWith("Posts").Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM