Selenium 在页面中找不到元素

塞萨尔

我正在尝试让 selenium 按下此页面上的下载按钮。

关联

我一直在尝试使用

mod = browser.find_element_by_css_selector('#method_free').click()

但我得到了错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
游轮潘迪

您基本上需要显式等待,因为 HTML DOM 中的 id 是唯一的。

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get("https://modsbase.com/yjdfcs34gxix/1863514508_BetterCheats.zip.html")
wait.until(EC.element_to_be_clickable((By.ID, "method_free"))).click()

进口:

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

请注意,一旦您clickdownloadanew tab上打开,要与 a 中的 web 元素进行交互new window,您就需要切换。

all_handles = driver.window_handles
driver.switch_to.window(all_handles[1])

然后您可以与新的窗口项进行交互。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章