我是 selenium 的初学者,如果问题很愚蠢(很愚蠢),我深表歉意:) 我需要在 https://www.netflix.com/ 上找到速度测试链接然后点击它。
我试过按文本和其他一些选项进行搜索。但似乎没有任何效果,我不知道为什么。
from selenium import webdriver
from selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.netflix.com/")
driver.implicitly_wait(10)
elem = driver.find_element_by_link_text("Speed test")
elem.click()
NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“链接文本”,“选择器”:“登录”} ( session 信息:chrome=75.0.3770.142)
最佳答案
文本为Speed Test 的元素不在Viewport 中因此您需要为所需的 元素引入 WebDriverWait 以使其可点击 ()
,并且您可以使用以下 Locator Strategy :
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='footer-link']/span[text()='Speed Test']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
关于python-3.x - 如何在 python 中使用 selenium 定位并单击 netflix 上的 "speed test"链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57128028/