簡體   English   中英

在Python、select HTML頁面元素內容中使用selenium與xpath?

[英]Using selenium in Python, select HTML page element content with xpath?

在 Xpath Helper 插件中,我能夠獲取 HTML 標簽內容:

QUERY://div[@id="cardModel"]/div[@class="modal-dialog"]/div[@class="modal-content"]//tr[1]/td[1]//tr/td[2]/div/span/text()

RESULTS (1):Enrico

結果是:

Enrico

但在 Python 中:

from selenium import webdriver
from lxml import etree

driver = webdriver.Chrome()
detailUrl = 'https://www.enf.com.cn/3d-energy-1?directory=panel&utm_source=ENF&utm_medium=perc&utm_content=22196&utm_campaign=profiles_panel'
driver.get(detailUrl)
html_ele_detail = etree.HTML(driver.page_source)
time.sleep(5)

companyPhone = html_ele_detail.xpath('//div[@id="cardModel"]/div[@class="modal-dialog"]/div[@class="modal-content"]//tr[1]/td[1]//tr/td[2]/div/span/text()')
print("companyPhone = ", companyPhone)

companyPhone顯示為空,怎么回事?謝謝大家解決了這個問題

由於您已經在使用selenium庫,因此不需要使用etree庫。

對於這個應用程序selenium庫就足夠了

請參閱下面的示例並根據您的目的進行調整:

from selenium import webdriver


driver = webdriver.Chrome()
detailUrl = 'your url here'
driver.get(detailUrl)

web_element_text = driver.find_element_by_xpath('your xpath directory here').text

print(web_element_text)

單擊此處查看另一個主題中的一些其他示例

讓我知道這是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM