【问题标题】:Selenium cannot find element in pageSelenium 在页面中找不到元素
【发布时间】:2021-09-15 21:50:59
【问题描述】:

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

Link

我一直在尝试使用

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

但我得到了错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    基本上你需要显式等待,因为 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
    

    请注意,一旦您在downloadnew tab 上打开click,以与new window 中的网络元素进行交互,您就需要切换。

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

    然后您就可以与新的 windows 项目进行交互了。

    【讨论】:

    • 成功了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2014-09-26
    • 2023-03-24
    • 2021-03-08
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多