How do I wait until pages load before clicking an element using Selenium and Python? [duplicate]
How do I wait until pages load before clicking an element using Selenium and Python? [duplicate]
This question already has an answer here:
I'm new to python, and I'm trying to create code that clicks a button as soon as the page loads, but not after. Every thing I have tried has failed because the page will sometimes take too long to load, and the button will end up not being clicked (or it'll be clicked too early). Does anyone know how I can get the element to be clicked only when it loads in, and immediately when it loads in? One post I read said that the way I'm doing it currently (as seen below) worked for a bunch of other people, but it doesn't work for me.
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@id='stylesSection']/div[2]/div[2]/a"))
)
finally:
elem2 = driver.find_element_by_xpath("//*[@id='stylesSection']/div[2]/div[2]/a")
eleme2.click()
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@id='pageContent']/div[3]/div/div[1]/div[2]/div[2]/a"))
)
finally:
elem3 = driver.find_element_by_xpath("//*[@id='pageContent']/div[3]/div/div[1]/div[2]/div[2]/a")
elem3.click()
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@id='buyUsedPanel']/div[2]/div/div[3]/a"))
)
finally:
elem4 = driver.find_element_by_xpath("//*[@id='buyUsedPanel']/div[2]/div/div[3]/a")
elem4.click()
edit:
I tried to extend the time to 100 and then 500, but it keeps giving me this error, no matter what I do. Any idea how to stop this from happening?
File "tester3.py", line 205, in <module>
elem2.click()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a class="big-button-one style-link" href="/kia/optima/2013/lx-hybrid-sedan-4d/options/?vehicleid=385282&intent=buy-used&mileage=70160&pricetype=retail&modalview=false">...</a> is not clickable at point (422, 518). Other element would receive the click: <div id="socialToolbar" class="hideForPrint social-toolbar ">...</div>
File "tester3.py", line 205, in <module>
elem2.click()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a class="big-button-one style-link" href="/kia/optima/2013/lx-hybrid-sedan-4d/options/?vehicleid=385282&intent=buy-used&mileage=70160&pricetype=retail&modalview=false">...</a> is not clickable at point (422, 518). Other element would receive the click: <div id="socialToolbar" class="hideForPrint social-toolbar ">...</div>
The other related posts are in javascript, and I don't know how to use that code in my python program.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I updated what I had written earlier. I tried extending it, but it gave me the same error that it gives me every once in a while. Either it works, or I get this error. Even if the time was at 500, when it failed, it would give me this error. (Even though it did work sometimes)
– Jacob
Sep 7 '18 at 3:59
Have you tried to increase the wait time? Since you told that sometimes it takes a long time to load the page this could be the problem. (Don't be scared to increase the wait time, if it finds the button before the wait time it will proceed before the max wait time)
– André Roggeri Campos
Sep 6 '18 at 23:38