0

I am using the wait for visibility of an element function before clicking on that element. However, there are multiple elements in the HTML that satisfy the search I am using for my element, which is how I want it. How do I use the wait for visibility to wait for just the Nth element of the multiple elements to become visible?

For example, here is a portion of my code, the result of which would be to wait for visibility of the first of the multiple elements and then click on it:

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

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

Start_Element = wait.until(expected_conditions.visibility_of_element_located((By.XPATH, "//*[@class='start' and starts-with(text(), '7')]")))
Start_Element.click()

I want to be able to wait for just the 1st, 2nd, 3rd, etc. Start_Element to be visible and then click on it.

I know that if I just wanted to click on the Nth element, I could do something like this, where the [n] at the end decides which element I click:

driver.find_elements_by_xpath("//*[@class='start' and starts-with(text(), '7')]")[0].click()

But, what I cannot figure out is how to incorporate that into the wait for visibility function to wait for visibility of just the nth element before clicking on it.

Thanks!

mhopki3
  • 11
  • 1

1 Answers1

-1

You can modify your xpath to look for nth element as below:

eleXpath = "(//*[@class='start' and starts-with(text(), '7')])["+str(3)+"]"
Start_Element = wait.until(expected_conditions.visibility_of_element_located((By.XPATH, eleXpath )))
Start_Element.click()

Now above code will wait for 3rd element to be loaded Of all elements located by //*[@class='start' and starts-with(text(), '7')]

rahul rai
  • 2,260
  • 1
  • 7
  • 17