Wait until button is clicked in Selenium WebDriver to click on next button?
You are looking for Selenium Waits. Essentially, you want to click a button and wait for the other button to be present, then click that. A similar question was answered here:Selenium waitForElement.
You can do that like so (untested code):
import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
with contextlib.closing(webdriver.Firefox()) as driver:
driver.get('http://example.com')
wait = ui.WebDriverWait(driver,10)
driver.find_element_by_xpath('//*[@id="add-remove-buttons"]/input').click()
# Wait until the element appears
wait.until(lambda driver: driver.find_element_by_xpath('//*[@id="cart"]/a[2]'))
driver.find_element_by_xpath('//*[@id="cart"]/a[2]').click()
You will probably need to play around with this. I find that whenever I'm using wait
it takes a while to get it right. You can use driver.save_screenshot
to debug.
Related videos on Youtube
Teddy
Updated on May 25, 2022Comments
-
Teddy almost 2 years
I'm at the point in my program where it will click a button within the browser and within that page, another button should appear. After that button appears, my program will immediately run the next action to click the next button. I'm getting this error currently:
ElementNotVisibleException: Message: element not visible
Therefore, I'm assuming I'm calling the action to click the next button before that button appears. My question is what would I do to make my program wait until I can click on the button, to click on the button?
driver.find_element_by_xpath('//*[@id="add-remove-buttons"]/input').click() driver.find_element_by_xpath('//*[@id="cart"]/a[2]').click()
This is what the code looks like at the bottom of my program. I need to be able to wait until the second action is possible to complete the second action. Thanks for all the help!
-
JeffC over 6 yearsWhat have you googled? What have you tried? Surely since you know you want to wait you can google
python Selenium wait for element
and try some code in python since answers are readily available?
-