在python中使用Selenium WebDriver进行显式等待来查找元素 [英] Finding element with explicit wait using selenium webdriver in python

查看:275
本文介绍了在python中使用Selenium WebDriver进行显式等待来查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用链接文本查找元素,我正在使用以下代码

I am trying to find the element with the link text, I am using following code

handle = driver.window_handles
#handle for windows
driver.switch_to.window(handle[1])
#switching to new window
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))

我正在追踪跟踪

Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
raise TimeoutException(message)
TimeoutException: Message: ''

我要选择的元素的HTML是

HTML of the element I am trying to select is

<a href="/Kevin-Rose/followers">Followers <span class="profile_count">43,799</span></a>

我该如何解决这个问题?

How can I solve this problem??

推荐答案

如果使用

If you use By.LINK_TEXT, there should be a link with exactly that text: Followers, but you have Followers 43,799.

在您的情况下,您应该使用 By.PARTIAL_LINK_TEXT 代替:

In your case, you should use By.PARTIAL_LINK_TEXT instead:

wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Followers')))

更新,这是工作示例:

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()  # CHANGEME
driver.get('http://www.quora.com/Kevin-Rose')
element = WebDriverWait(driver, 2).until(
    EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Followers"))
)
element.click()

这篇关于在python中使用Selenium WebDriver进行显式等待来查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆