Python xpath获取一个简单的div id=";xyz";含硒

Python xpath获取一个简单的div id=";xyz";含硒,python,selenium,selenium-webdriver,xpath,Python,Selenium,Selenium Webdriver,Xpath,我正在挖掘的html(上面没有iframe)是: 我得到这个错误: 4444444444444444 <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="c8da2a89-63fd-4dce-bdb5-28fdf0e67b01", element="b9be2828-115e-4965-8ebc-b75c09236193")> e: Message: Unable to locate ele

我正在挖掘的html(上面没有iframe)是:

我得到这个错误:

 4444444444444444 <selenium.webdriver.firefox.webelement.FirefoxWebElement
 (session="c8da2a89-63fd-4dce-bdb5-28fdf0e67b01",
  element="b9be2828-115e-4965-8ebc-b75c09236193")>

 e: Message: Unable to locate element: //div[@id='app']
4444
e:消息:无法找到元素://div[@id='app']

有没有办法让驱动程序返回原始html,这样我就可以检查我认为我拥有的DOM try是否就是我实际拥有的DOM try

检查元素是否存在于任何
iframe
中。如果存在,则需要先将其切换

如果没有,那就试试看

诱导
WebDriverWait
()和
位于的元素的可见性(

您需要导入以下库

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

检查元素是否存在于任何
iframe
中。如果存在,则需要先将其切换

如果没有,那就试试看

诱导
WebDriverWait
()和
位于的元素的可见性(

您需要导入以下库

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

如果您的用例是将元素的原始html返回给我,那么您必须为与
get\u属性(“outerHTML”)
结合的
元素的
可见性诱导WebDriverWait,并且您可以使用以下任一项:

  • 使用
    CSS\u选择器

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.container-fluid#app"))).get_attribute("outerHTML"))
    
  • 使用
    XPATH

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='container-fluid' and @id='app']"))).get_attribute("outerHTML"))
    
  • 注意:您必须添加以下导入:

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

如果您的用例是将元素的原始html返回给我,那么您必须使用
get\u属性(“outerHTML”)
为元素的
可见性引入WebDriverWait,并且您可以使用以下任一项:

  • 使用
    CSS\u选择器

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.container-fluid#app"))).get_attribute("outerHTML"))
    
  • 使用
    XPATH

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='container-fluid' and @id='app']"))).get_attribute("outerHTML"))
    
  • 注意:您必须添加以下导入:

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

检查元素是否存在于任何iframe中??查看元素的输出
444444@user3313834这听起来像是一个错误。编辑您的问题并询问实际问题,而不是寻求帮助解决问题。你想做什么?@DebanjanB我已经编辑了这个问题,以确定我想做的是什么。检查元素是否存在于任何iframe中??查看元素被发现的输出
444444@user3313834这听起来像是一个错误。编辑您的问题并询问实际问题,而不是寻求帮助解决问题。你想做什么?@DebanjanB我已经编辑了这个问题,以明确我想做什么