我正在尝试使用链接文本找到元素,我正在使用以下代码
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是
<a href="/Kevin-Rose/followers">Followers <span class="profile_count">43,799</span></a>
我该如何解决这个问题?
答案 0 :(得分:4)
如果您使用By.LINK_TEXT
,则应该有一个完全相同的文字链接:Followers
,但您有Followers 43,799
。
在您的情况下,您应该使用By.PARTIAL_LINK_TEXT
代替:
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()
答案 1 :(得分:0)
欢呼
from selenium import webdrivercode
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://somedomain/url_that_delays_loading")
#Follow below syntax
element = WebDriverWait(driver, 10)
element.until(EC.presence_of_element_located((By.ID, '--webElement--')))