Reputation: 35
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
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.
Upvotes: 0
Views: 254
Reputation: 19949
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
Upvotes: 1
Reputation: 193108
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
You can find a couple of relevant discussions on NoSuchElementException in:
Upvotes: 0