Selenium 找不到现有元素
Existing element can't be found by Selenium
我试图通过它的 id 和 xpath 在页面中找到一个元素,但在每种情况下我都得到相同的错误消息。我也尝试通过使用下面的代码显示的文本来查找元素,但问题仍然存在:
driver.find_elements_by_xpath("//*[contains(text(), 'Uma ou mais sondas DP')]")
我确保等到页面完全加载后再尝试查找元素。
对于可能导致此问题的原因有什么想法或建议吗?
这是访问源代码的存储库 link: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
编辑:正如我在评论部分提到的,该元素位于 iframe 中,如图 here 所示。但是我得到了一个 TimeoutException 尝试在 find_element 命令之前使用以下代码,正如建议的那样:
iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//iframe")))
driver.switch_to.frame(iframe)
我尝试使用 find_elements_by_tag_name 方法在源代码中查找 iframe 元素,但它返回一个空列表。
要定位元素,您可以使用以下任一方法 :
使用css_selector
:
element = driver.find_element(By.CSS_SELECTOR, "span.lsTextView[ct='TV'][title='Nome']")
使用xpath
:
element = driver.find_element(By.XPATH, "span[@ct='TV' and @title='Nome'][starts-with(., 'Uma ou mais sondas DP')]")
理想情况下,要找到需要为 visibility_of_element_located()
引入 WebDriverWait 的元素,您可以使用以下任一方法 :
使用CSS_SELECTOR
:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.lsTextView[ct='TV'][title='Nome']")))
使用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')]")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于 的一些相关讨论:
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()
您必须先切换到 iframe 才能与其中的元素交互,
然后切换回来与 iframe 之外的元素进行交互
我试图通过它的 id 和 xpath 在页面中找到一个元素,但在每种情况下我都得到相同的错误消息。我也尝试通过使用下面的代码显示的文本来查找元素,但问题仍然存在:
driver.find_elements_by_xpath("//*[contains(text(), 'Uma ou mais sondas DP')]")
我确保等到页面完全加载后再尝试查找元素。
对于可能导致此问题的原因有什么想法或建议吗?
这是访问源代码的存储库 link: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
编辑:正如我在评论部分提到的,该元素位于 iframe 中,如图 here 所示。但是我得到了一个 TimeoutException 尝试在 find_element 命令之前使用以下代码,正如建议的那样:
iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//iframe")))
driver.switch_to.frame(iframe)
我尝试使用 find_elements_by_tag_name 方法在源代码中查找 iframe 元素,但它返回一个空列表。
要定位元素,您可以使用以下任一方法
使用
css_selector
:element = driver.find_element(By.CSS_SELECTOR, "span.lsTextView[ct='TV'][title='Nome']")
使用
xpath
:element = driver.find_element(By.XPATH, "span[@ct='TV' and @title='Nome'][starts-with(., 'Uma ou mais sondas DP')]")
理想情况下,要找到需要为 visibility_of_element_located()
引入 WebDriverWait 的元素,您可以使用以下任一方法
使用
CSS_SELECTOR
:element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.lsTextView[ct='TV'][title='Nome']")))
使用
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')]")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于
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()
您必须先切换到 iframe 才能与其中的元素交互,
然后切换回来与 iframe 之外的元素进行交互