【问题标题】:Python Selenium move to elementPython Selenium 移动到元素
【发布时间】:2023-01-20 17:51:23
【问题描述】:
driver = webdriver.Firefox() 
driver.maximize_window() 
url = r"https://www.nba.com/stats/players/traditional" 
driver.get(url) 

advanced = driver.find_element(By.XPATH, r'//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a')
action = ActionChains(driver)
action.move_to_element(advanced)
advanced.click()

返回错误总是:元素无法滚动到视图中。

我已经尝试过此代码的其他版本,包括:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a'))).click()

请帮忙,谢谢你了

【问题讨论】:

    标签: python selenium xpath webdriverwait


    【解决方案1】:

    尝试使用:

    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a')))
    
    driver.execute_script("arguments[0].scrollIntoView();", element)
    
    element.click()
    

    将元素滚动到视图中。 resource

    【讨论】:

    • 引发 TimeoutException(消息、屏幕、堆栈跟踪)selenium.common.exceptions.TimeoutException:消息:
    • 您好,谢谢您的回复。执行此代码时,这是我收到的返回消息。 “消息:”不返回任何内容。
    【解决方案2】:

    您需要接受 cookie 同意按钮,然后单击切换以展开下拉列表,然后选择该元素。

    driver.get("https://www.nba.com/stats/players/traditional")
    #accept cookie consent
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
    time.sleep(1)
    #expand
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//div[@class='StatsQuickNavSelector_nav__JzoME']/button)[last()]"))).click()
    #click on specific item
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Advanced']"))).click()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2018-11-07
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多