Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用硒提取第一代婴儿_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 用硒提取第一代婴儿

Python 用硒提取第一代婴儿,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我想用文本提取第一个span,提取此文本。已尝试: element.find_element_by_css_selector(".moreContent span:nth-child(1)").text.strip('"') 这不起作用,我不知道为什么。输出只是空的 <p class="mainText"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. <span

我想用文本提取第一个
span
,提取此文本。已尝试:

element.find_element_by_css_selector(".moreContent span:nth-child(1)").text.strip('"')
这不起作用,我不知道为什么。输出只是空的

<p class="mainText">
  Lorem Ipsum is simply dummy text of the printing and typesetting industry.
  <span class="moreEllipses">…&nbsp;</span>
  <span class="moreContent">
    <span> Extract this text </span>
    <span class="link moreLink">Show More</span>
  </span>
</p>

Lorem Ipsum只是印刷和排版行业的虚拟文本。 … 提取此文本 显示更多

但是我得到了这个结果,因此Selenium找到了元素,但为什么输出为空:

<selenium.webdriver.remote.webelement.WebElement (session="e7012b303842651848aa0b0e40f5d5c1", element="df5644e9-fc98-4300-ad86-9ff433154d82")>

编辑:


我通过点击ShowMore按钮解决了这个问题。由于某些原因,即使在页面中不可见,我也无法提取内容。

根据您的CSS选择器,您的目标似乎在下面

<span> Extract this text </span>

示例代码:

element = driver.find_element_by_xpath("(//p[@class='mainText']//span[@class='moreContent']/span)[1]").text

根据您的CSS选择器,您的目标似乎如下

<span> Extract this text </span>

示例代码:

element = driver.find_element_by_xpath("(//p[@class='mainText']//span[@class='moreContent']/span)[1]").text

要从第一个
中提取文本,即提取此文本,您需要为位于()的元素的
可见性引入WebDriverWait,并且您可以使用以下任一选项:

  • 使用
    CSS\u选择器
    文本
    属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p.mainText span.moreContent>span"))).text)
    
  • 使用
    XPATH
    get\u attribute()
    方法:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[@class='mainText']//span[@class='moreContent']/span"))).get_attribute("innerHTML"))
    
  • 注意:您必须添加以下导入:

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

要从第一个
中提取文本,即提取此文本您需要为位于()的元素的
可见性引入WebDriverWait,并且您可以使用以下任一项:

  • 使用
    CSS\u选择器
    文本
    属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p.mainText span.moreContent>span"))).text)
    
  • 使用
    XPATH
    get\u attribute()
    方法:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[@class='mainText']//span[@class='moreContent']/span"))).get_attribute("innerHTML"))
    
  • 注意:您必须添加以下导入:

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

您正在寻找哪种元素 或提取此文本?@ShubhamJain“提取此文本”下面我给出的Xpath答案有效吗?@ShubhamJain,不幸的是无效。找不到元素此元素是否在任何框架标记内?要查找哪个元素 或提取此文本?@ShubhamJain“提取此文本”下面我给出的Xpath答案有效吗?@ShubhamJain,不幸的是无效。找不到元素此元素是否位于任何框架标记内?