简体   繁体   English

Selenium Python - Text() 适用于 Xpath 但不适用于 CSS_Selector

[英]Selenium Python - Text() works with Xpath but does not with CSS_Selector

I am trying to webscrape a House listing website in Quebec and I encounter a problem.我正在尝试对魁北克的房屋列表网站进行网络抓取,但遇到了问题。

When I use Xpath I manage to extract the phone number, but when I use CSS selectors my variable is Blank.当我使用 Xpath 时,我设法提取了电话号码,但是当我使用 CSS 选择器时,我的变量是空白的。

However I am pretty sure that I am aiming at the right spot I even used the css tool in chrome to verify.但是我很确定我的目标是正确的,我什至使用了 chrome 中的 css 工具来验证。

The reason why i would need to use CSS selector is beacause the page is dynamic and not every listing is same, which means that sometimes I am not able to point out the real location using xpath like that我需要使用 CSS 选择器的原因是因为页面是动态的并且并非每个列表都是相同的,这意味着有时我无法像这样使用 xpath 指出真实位置

An example would be this URL: https://duproprio.com/fr/monteregie-rive-sud-montreal/ange-gardien-de-rouville/ferme-fermette-a-vendre/hab-707-rang-saint-charles-1008751一个例子是这个 URL: https ://duproprio.com/fr/monteregie-rive-sud-montreal/ange-gardien-de-rouville/ferme-fermette-a-vendre/hab-707-rang-saint-charles -1008751

EXAMPLE THAT WORKS有效的例子

        try:
            house_phone = deal_box.find_element_by_xpath("/html/body/main/div[1]/div/div[2]/div[1]/div[1]/div[2]/section/div[1]/div[1]/div/a").text.strip()
        except: house_phone = "N/D"

EXAMPLE THAT DOESNT WORK不起作用的例子

       try:
           house_phone = deal_box.find_element(By.CSS_SELECTOR, 'a.gtm-listing-link-contact-owner-phone').text.strip()
       except: house_phone = "N/D"

Thank you谢谢

You can pull it from the textContent attribute您可以从 textContent 属性中提取它

deal_box.get(url)
try:
    house_phone = deal_box.find_element(By.CSS_SELECTOR, 'a.gtm-listing-link-contact-owner-phone').get_attribute('textContent').strip()
except Exception as e:
    print(e)
    house_phone = "N/D"
    
house_phone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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