-1

Hi i need to check for an element using python Selenium. If the element is found then i in need to click on it or else click on the next page and search the element again.This continues until the element is found.

The code i have used is :

 1. element_path="//span[contains(text(),'ABC Company')]" 
 2. while True: 
 3.  if(driver.find_elements_by_xpath(element_xpath)): 
          driver.find_element_by_xpath(element_xpath).click() 
 4.  else: driver.find_element_by_xpath("xpath to goto next page").click()

But this code doesn't work as I'm getting an error: no such element: {"method":"xpath","selector":"//span[contains(text(),'ABC Company')]"}

Can this looping be done in any other method? Thanks in Advance

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • This can work, but you need to wait for element to be rendered on the front end. See https://stackoverflow.com/questions/59130200/selenium-wait-until-element-is-present-visible-and-interactable for more info. – Mate Mrše Jun 29 '21 at 12:03
  • what's site address? stale element reference error, no such element error is dynamic problem – 4rigener Jun 29 '21 at 13:28
  • @MateMrše i have added a **driver.implicitely_wait(1).** – Dreamcatcher_AR Jun 29 '21 at 13:37
  • @Dreamcatcher_AR 1 second might not be enough in all cases. Explicit waits are generally speaking a better solution. But if you choose to use implicit waits, try with 5 seconds just to be safe the button has time to be rendered. – Mate Mrše Jun 29 '21 at 14:19

1 Answers1

0

I am not sure if you need to refresh the page also, but I think you should check for len() function that will have find_elements with the xpath you have mentioned.

try this instead :

while True:
    try:
        if len(driver.find_elements(By.XPATH, "//span[contains(text(),'ABC Company')]")) > 0:
            driver.find_element_by_xpath("//span[contains(text(),'ABC Company')]").click()
            break
        else:
            driver.find_element_by_xpath("xpath to goto next page").click()
            break
    except:
        pass
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thank you for the Reply. This doesnt work. im getting : **Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adaptor failed.** – Dreamcatcher_AR Jun 29 '21 at 13:35
  • and if i remove the try and except im getting **no such element: {"method":"xpath","selector":"//span[contains(text(),'ABC Company')]"}** – Dreamcatcher_AR Jun 29 '21 at 13:36
  • When you print `len(driver.find_elements(By.XPATH, "//span[contains(text(),'ABC Company')]"))` this what is the value ? – cruisepandey Jun 29 '21 at 13:46
  • i changed the code a bit: ``element_path="//span[contains(text(),'ABC Company')]"`` ``print(len(driver.find_elements_by_xpath(element_xpath)))`` ``` and im not getting anythin in the output as length and in the above format if i do im getting the same error – Dreamcatcher_AR Jun 29 '21 at 14:43
  • and when i tried with ``element_path="//span[contains(text(),'123')]" `` ``print(len(driver.find_elements_by_xpath(element_xpath)))`` where **123** is an element in the 1st page: im getting **TypeError: object of type 'WebElement' has no len()** – Dreamcatcher_AR Jun 29 '21 at 14:51