簡體   English   中英

使用 selenium 的 Xpath 錯誤

[英]Xpath wrong using selenium

from selenium import webdriver           
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from csv import writer


options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
wait = WebDriverWait(driver, 20) 
            
            
URL = 'https://www.askgamblers.com/online-casinos/reviews/yukon-gold-casino-casino'
driver.get(URL) 



data=driver.find_elements(By.XPATH,"//section[@class='review-text richtext']")
        
for row in data:
    try:
        para0= row.find_element(By.XPATH,"//h2[text()[contains(.,'Games')]]/following-sibling::p[following::h2[text()[contains(.,'Support')]]]").text
    except:
        pass  
    
    print(para0)     

我希望他們只收集Games的數據,但他們也獲得Virtual Games的數據,所以我們如何限制只獲取Games數據的contains method ,請推薦任何解決方案,因為這些是頁面鏈接https://www.askgamblers。 com/online-casinos/reviews/yukon-gold-casino-casino

只想要這些

在此處輸入圖像描述

不要得到這些virtual game的文字在此處輸入圖像描述

[contains(.,'Games')]將同時匹配GamesVirtual Games
你可以在這里做的是:

  1. 使用equals而不是contains ,如下所示:
"[text()='Games']"
  1. 或使用starts-with
"[starts-with(text(), 'Games')]"

所以這一行para0= row.find_element(By.XPATH,"//h2[text()[contains(.,'Games')]]/following-sibling::p[following::h2[text()[contains(.,'Support')]]]").text可以改成

para0= row.find_element(By.XPATH,"//h2[text()='Games']/following-sibling::p[following::h2[contains(.,'Support')]]").text

要么

para0= row.find_element(By.XPATH,"//h2[starts-with(text(), 'Games')]/following-sibling::p[following::h2[contains(.,'Support')]]").text

暫無
暫無

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

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