Home > Software design >  Why do I get the error "Element could not be scrolled into view" unless I manually scroll
Why do I get the error "Element could not be scrolled into view" unless I manually scroll

Time:01-31

I wrote a python script that goes to a site, and interacts with some dropdowns. It works perfectly fine if after I run the script, quickly make the browser instance full screen so that the elements are in view. If I don't do that, I get the error "Element could not be scrolled into view".

Here is my script:

from selenium import webdriver
driver = webdriver.Firefox()

driver.get("https://example.com")
driver.implicitly_wait(5)

yearbtn = driver.find_element("id", "dropdown_year")
yearbtn.click()
year = driver.find_element("css selector", '#dropdown_ul_year li:nth-child(5)')
year.click()

makebtn = driver.find_element("id", "dropdown_make")
makebtn.click()
make = driver.find_element("css selector", '#dropdown_ul_make li:nth-child(2)')
make.click()

modelbtn = driver.find_element("id", "dropdown_model")
modelbtn.click()
model = driver.find_element("css selector", '#dropdown_ul_model li:nth-child(2)')
model.click()

trimbtn = driver.find_element("id", "dropdown_trim")
trimbtn.click()
trim = driver.find_element("css selector", '#dropdown_ul_trim li:nth-child(2)')
trim.click()

vehicle = driver.find_element("css selector", '#vehiclecontainer > div > p')
vdata = driver.find_element("css selector", '.top-sect .tow-row:nth-child(2)')

print("--------------")
print("Your Vehicle: "   vehicle.text)
print("Vehicle Data: "   vdata.text)
print("--------------")
print("")

driver.close()

Like I said, it works fine if I make the browser full-screen (or manually scroll) so that the elements in question are in view. It finds the element, so what's the issue here? I've tried both Firefox and Chrome.

CodePudding user response:

Generally opens in maximized mode. Incase for specific version of it doesn't, the best practice is to open it in maximized mode as follows:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument("start-maximized")
driver = webdriver.Firefox(options=options)

However in somecases the desired element may not get rendered within the due coarse, so you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

vehicle = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#vehiclecontainer > div > p")))
vdata = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".top-sect .tow-row:nth-child(2)")))

print("--------------")
print("Your Vehicle: "   vehicle.text)
print("Vehicle Data: "   vdata.text)
print("--------------")
print("")
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

CodePudding user response:

I'm not sure what all is happening on the page in question. I use a generic method to take care of common actions like click(), find(), etc. that contain a wait, scroll, and whatever else is useful for that action. I've put an example below. It waits for the element to be clickable (always a best practice), scrolls to the element, and then clicks it. You may want to tweak it to your specific use but it will get you pointed in the right direction.

def click(self, locator):
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable(locator))
    driver.execute_script("arguments[0].scrollIntoView();", element)
    element.click()

I would suggest you try this and then convert all of your clicks to use this method and see if that helps, e.g.

click((By.ID, "dropdown_year"))

or

locator = (By.ID, "dropdown_year")
click(locator)
  • Related