Home > front end >  Python Selenium move to element
Python Selenium move to element

Time:01-21

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()

return error is always: element could not be scrolled into view.

I've tried other versions of this code including:

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()

Please help, Thank you already

CodePudding user response:

You need to accept the cookie consent button and then click on of toggle to expand the dropdown and then select the element.

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()

CodePudding user response:

Try using:

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()

To scroll the element into view. resource

  • Related