Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x 如何使用selenium/python访问HTML文本?_Python 3.x_Selenium Webdriver_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 3.x 如何使用selenium/python访问HTML文本?

Python 3.x 如何使用selenium/python访问HTML文本?,python-3.x,selenium-webdriver,xpath,css-selectors,webdriverwait,Python 3.x,Selenium Webdriver,Xpath,Css Selectors,Webdriverwait,我有如下HTML标记: <div class="mt-md common__EiReviewTextStyles__allowLineBreaks"> <p class="strong">Pros</p> <p>This is the text to be captured</p></div> 专业人士 这是要捕获的文本 我想在Python上使用selenium访问文本“这是要捕获的文本” 关于如何做到

我有如下HTML标记:

<div class="mt-md common__EiReviewTextStyles__allowLineBreaks">
    <p class="strong">Pros</p>
    <p>This is the text to be captured</p></div>

专业人士

这是要捕获的文本

我想在Python上使用selenium访问文本“这是要捕获的文本”


关于如何做到这一点,你有什么想法吗?

使用
。通过xpath查找元素,如下所示:

driver.find_element_by_xpath('//div[contains(@class, "mt-md")]//p[@class="strong"]//following-sibling::p[text()]').text
或者,如果您想获得
div
中的所有文本,请使用

driver.find_element_by_css_selector('div.mt-md.common__EiReviewTextStyles__allowLineBreaks').text

要提取第二个
标记内的文本,即这是要捕获的文本,您必须为位于()的元素的
可见性诱导WebDriverWait,并且您可以使用以下任一项:

  • 使用
    CSS\u选择器
    get\u属性()

  • 使用
    XPATH
    text

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='mt-md common__EiReviewTextStyles__allowLineBreaks']//following-sibling::p[2]"))).text)
    

有大量的博客和网站可以为您提供网络抓取的示例。这个问题不太适合当前形式的stackoverflow。我建议尝试解决这个问题,如果它不起作用,那么展示你的尝试,人们会帮助你=“”

Pros

这是要捕获的文本

“你可以尝试从bs4导入BeautifulSoup
导入BeautifulSoup=BeautifulSoup(s)打印(soup.text)
我已经尝试通过css获取元素”(.mt-md…)然后通过类名称('strong')获取元素。这只提供“Pros”作为输出。我想在输出中也获取下一行
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='mt-md common__EiReviewTextStyles__allowLineBreaks']//following-sibling::p[2]"))).text)