【问题标题】:How to click element using selenium web driver(Python)如何使用 selenium Web 驱动程序(Python)单击元素
【发布时间】:2020-10-29 11:22:31
【问题描述】:

我正在尝试通过使用要单击的元素的 ID 来单击使用 selenium chromedriver 的元素。

我想从以下网页单击“2020 年”:“https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan”

我尝试了以下代码。

driver = webdriver.Chrome(executable_path=ChromeDriver_Path, options = options)
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')

Id = "ctl00_ContentPlaceHolder1_gvMajorIncident_ct123_lbtnYear" ### Id of an Element 2020
wait = WebDriverWait(driver, 20)  ##Wait for 20 seconds
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
time.sleep(10)

但不幸的是,这会产生如下错误:

    element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
  File "C:\Users\Pavan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

请任何人帮助我...谢谢;

【问题讨论】:

    标签: python-3.x selenium-webdriver


    【解决方案1】:

    我不知道您的导入是否正确。此外,您的代码不会滚动到页面底部。使用这个。

    import os
    from selenium.webdriver.support import expected_conditions as EC
    import time
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    
    driver = webdriver.Chrome(executable_path='driver path')
    driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
    driver.maximize_window()
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(3)
    element = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ctl00_ContentPlaceHolder1_gvMajorIncident_ctl23_lbtnYear"]')))
    element.click()
    

    【讨论】:

      猜你喜欢
      • 2019-05-04
      • 2018-07-24
      • 2018-01-22
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      相关资源
      最近更新 更多