Reputation: 55
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!
Upvotes: 2
Views: 19069
Reputation: 193088
As per your Question, we are trying to invoke click()
method on both the buttons so we will induce ExplicitWait
i.e. WebDriverWait
with expected_conditions
as element_to_be_clickable
as follows :
firstButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPATH,"//*[@id='add-remove-buttons']/input"))
firstButton.click()
secondButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.XPATH,"//*[@id='cart']/a[2]"))
secondButton.click()
Upvotes: 0
Reputation: 2614
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.
Upvotes: 2