【问题标题】:I want to click on a button with Selenium我想用 Selenium 点击一个按钮
【发布时间】:2021-06-05 21:04:52
【问题描述】:

我正在尝试网页抓取,我需要模拟点击按钮,我已经尝试过:

url = "https://apps5.mineco.gob.pe/transparencia/mensual/default.aspx?y=2021&ap=ActProy"
driver = driver = webdriver.Chrome()
driver.get(url)
nivelGob = driver.find_element_by_xpath('//*[@id="ctl00_CPH1_BtnTipoGobierno"]')
nivelGob.click()

并返回此错误:

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id="ctl00_CPH1_BtnTipoGobierno"]"} (会话信息:chrome=88.0.4324.190)

我一直在尝试通过 css 选择器、类名来查找元素,但什么也没有。

这是按钮:

我希望有人可以帮助我。非常感谢。

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    该网站实际上位于另一个框架内,因此您需要切换到该框架。试试这个:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    import time
    
    url = "https://apps5.mineco.gob.pe/transparencia/mensual/default.aspx?y=2021&ap=ActProy"
    driver = webdriver.Chrome()
    driver.get(url)
    time.sleep(3)
    frame = driver.find_element_by_id("frame0")
    driver.switch_to.frame(frame)
    w = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "ctl00_CPH1_BtnTipoGobierno"))).click()
    

    【讨论】:

    • 非常感谢,您的回答解决了我的问题。
    【解决方案2】:

    也许 DOM 还没有完全加载。尝试为您的驱动程序添加隐式等待

    driver.implicitly_wait(10) # seconds

    【讨论】:

    • 我刚刚尝试过,但返回同样的错误。
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2016-05-08
    • 2021-09-14
    • 2018-05-11
    相关资源
    最近更新 更多