简体   繁体   中英

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

In the Xpath Helper plugin, I was able to get the HTML tag content:

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

The result is:

Enrico

But in 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 shows empty, what's wrong?Thank you all for solving this problem

As you are already using the selenium library, you do not need to use etree library.

For this application selenium library is enough

see the example below and adapt for your purpose:

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)

See some other examples in another topic by clicking here

Let me know if this was helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM