【问题标题】:Having trouble getting these elements in xpath selenium?在 xpath selenium 中获取这些元素时遇到问题?
【发布时间】:2021-08-05 01:25:16
【问题描述】:

感谢堆栈溢出帮,我完成了很多工作。 这是我到目前为止所做的:

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from lxml import html
from time import sleep
import time
x = 0
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://www.rosegal.com/plus-size-tops-120/')
driver.maximize_window()
sleep(1)
product = driver.find_element_by_xpath('/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/div[2]/div[5]/ul[1]/li[1]/div[1]/div[2]/p[1]/a[1]').click()
sleep(1)
title= driver.find_element_by_xpath("/html[1]/body[1]/div[1]/section[1]/div[1]/div[2]/div[1]/h1[1]").text
price= driver.find_element_by_xpath("//b[contains(@class,'my_shop_price')]").text
description= driver.find_element_by_xpath("//LI[@class=' logsss_event_cl']").click()
material= driver.find_elements_by_xpath('//*[@id="page"]/section/div/div[2]/div[9]/div[2]/div/div[2]/div/text()[2]').text
sleep(2)
#color1s = driver.find_element_xpath('//*[@id="page"]/section/div/div[2]/div[9]/div[2]/div/div[2]/div').text()
sleep(3)
pat1= driver.find_element_by_xpath('/html[1]/body[1]/div[1]/section[1]/div[1]/div[2]/div[6]/a[1]/img[1]').click()

sleep(2)
color1 = driver.find_element_by_css_selector(".logsss_event_cl.itemAttr.current").get_attribute("data-value")
#link = driver.find_element_by_css_selector(".logsss_event_cl.itemAttr.current>img").get_attribute("src")
large = driver.find_element_by_xpath('//*[@id="select-attr-1"]/span[2]/a[1]').click()
measure = driver.find_element_by_xpath("//body/div[@id='page']/section[1]/div[1]/div[2]/div[8]/div[1]/p[1]/span[1]").text
input = driver.find_element_by_xpath("//input[contains(@class,'fl num logsss_event_cl')]").send_keys('99')
plus = driver.find_element_by_xpath('//*[@id="select-attr-1"]/span[2]/a[1]').click()
sleep(3)
#inventory =driver.find_element_by_id("//input[contains(@class,'fl num logsss_event_cl')]").gettext()
inventory = driver.find_element_by_xpath("//input[contains(@class,'fl num logsss_event_cl')]").get_attribute("value")
#a1X = driver.find_element_by_xpath('//*[@id="select-attr-1"]/span[2]/a[2]').click()
#color2 = driver.find_element_by_xpath(".logsss_event_cl.itemAttr:nth-of-type(2)").get_attribute("data-value")
pat2 = driver.find_element_by_xpath('/html[1]/body[1]/div[1]/section[1]/div[1]/div[2]/div[6]/a[2]/img[1]').click()

print(title, price, color1, inventory, measure, material)

需要一个 XPath 语句,它将为我提供材料、衣领、衬衫长度等... 这是我尝试过的一件事: material= driver.find_elements_by_xpath('//* [@id="page"]/section/div/div[2]/div[9]/div[2]/div/div[2]/div/text()[2]').text

这是代码:

     div class="content none" style="" xpath="1"> <link type="text/css" rel="stylesheet" 

     href="https://css.drlcdn.com/css/S2.css"><div class="xxkkk" style="">

    <div>  </div><div class="xxkkk">
    <div class="xxkkk20" style="">

    <strong style="">Style:</strong> 
    Fashion <br>             

    <strong style="">Material:</strong> 
    Polyester,Polyurethane <br>             

    <strong>Collar:</strong> 
    Spaghetti Strap <br>             

    <strong>Shirt Length:</strong> 
    Long <br> 
            
    <strong>Sleeve Length:</strong> 
    Half <br>             

    <strong>Sleeve Type:</strong> 
    Cold Shoulder,Flare Sleeve <br>             
 
    <strong>Pattern Type:</strong> 
    Butterfly <br>             

    <strong>Fabric Stretch:</strong> 
    High Stretch <br>             
 
    <strong>Season:</strong> 
    Spring,Summer <br>             

    <strong>Weight:</strong> 
    0.2700kg <br>             
    </div>
    
    </div>

   <div class="xkclear"></div>

【问题讨论】:

    标签: selenium xpath element


    【解决方案1】:

    从您发布的 HTML 来看,这似乎可行:

    //strong/following-sibling::text()[1]
    

    或者,如果您想搜索特定标签,例如 Weight:,您可以这样做

    //strong[text()="Weight:"]/following-sibling::text()[1]
    

    【讨论】:

    • 我尝试了这两个建议,但仍然没有给出文本。这是凝灰岩。
    【解决方案2】:

    我终于明白了。

      material= driver.find_element_by_xpath('//*[@id="page"]/section/div/div[2]/div[9]/div[2]/div/div[2]/div').text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2023-02-24
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多