0

I have tried to find an element in a page by its id and xpath, but in every case I got the same error message back. I also tried to find the element by the text displayed using the code below, but the problem persisted:

driver.find_elements_by_xpath("//*[contains(text(), 'Uma ou mais sondas DP')]")

I made sure to wait until the page was fully loaded before trying to find the elements.

Any ideas or suggestions on what could be causing this issue?

This is the repository link to access the source code: https://github.com/LucasC97/HTML-Source-Code

The element I'm trying to find with Selenium is this one (I could only find it using the Inspector from Firefox)

Error by ID

Error by XPath

Edit: As I mentioned in the comment section, the element is inside an iframe, as shown here. However I got a TimeoutException trying to use the following code before the find_element command, as suggested:

iframe=WebDriverWait(driver,  10).until(EC.presence_of_element_located((By.XPATH, "//iframe")))

driver.switch_to.frame(iframe)

I tried to use the find_elements_by_tag_name method to find the iframe elements in the source code, but it returned an empty list.

Lucas Cruz
  • 23
  • 5

2 Answers2

1
iframe=WebDriverWait(driver,  10).until(EC.presence_of_element_located((By.XPATH, "//iframe")))

driver.switch_to.frame(iframe)

driver.find_element_by_id("WFD6")


#REMAINING CODE TO INTERACT WITH ELEMENTS INISIDE IFRAME

#once done exit from iframe

driver.switch_to.default_content()

You have to switch to iframe first to interact with elements inside that ,

Then switch back to interact with elements outside the iframe

PDHide
  • 10,919
  • 2
  • 12
  • 26
  • I tried to use the method you suggested but it unfortunately didn't work. I'm having a hard time trying to understand what is going on. I know that there is are iframe elements in the source code and that one of them contain the element I'm trying to find using Selenium, but for some reason Selenium also can't find any iframe elements. I updated the description of the question to add this information. – Lucas Cruz Jan 14 '21 at 21:01
  • @LucasCruz is the element inside , iframe and add the html of the iframe also – PDHide Jan 14 '21 at 21:03
  • 1
    I finally managed to find the element I wanted to, the problem was that it was nested inside multiple frames. Thank you for your help! – Lucas Cruz Jan 15 '21 at 02:37
0

To locate the element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "span.lsTextView[ct='TV'][title='Nome']")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "span[@ct='TV' and @title='Nome'][starts-with(., 'Uma ou mais sondas DP')]")
    

Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.lsTextView[ct='TV'][title='Nome']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "span[@ct='TV' and @title='Nome'][starts-with(., 'Uma ou mais sondas DP')]")))
    
  • Note : You have to add the following imports :

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

References

You can find a couple of relevant discussions on NoSuchElementException in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217