![](/img/trans.png)
[英]How to retrieve the value of the attribute aria-label from element found using xpath as per the html using Selenium
[英]How to find aria -label element using contains in xpath
我正在尝试获取位于锚标记内但不是 href 的信息。 我想从 ebay 上的几个卖家那里提取评分。 在以下 HTML 代码中,您可以看到可以找到评分的位置。 有没有办法在不使用 href 的情况下获取有关“Bewertungspunktestand”(德语评分)的信息,因为 href 从卖家到卖家会发生变化。 此示例中的评分为 32。由于文本“Bewertungspunktestand”仅在这一行中,我认为可以让它搜索该文本并提取其中包含该文本的 aria-label。
这是我尝试过但没有成功的python代码:
尝试:trans = driver.find_element_by_xpath("//a[@aria-label='Bewertungspunktestand']") 除外:trans = '0'
这是 HTML 代码
<span class="mbg-l"> (<a href="http://feedback.ebay.de/ws/eBayISAPI.dll?ViewFeedback&userid=thuanhtran&iid=133585540546&ssPageName=VIP:feedback&ftab=FeedbackAsSeller&rt=nc&_trksid=p2047675.l2560" aria-label="Bewertungspunktestand: 32">32</a> <span class="vi-mbgds3-bkImg vi-mbgds3-fb10-49" aria-label="Gelber Stern für 10 bis 49 Bewertungspunkte" role="img"></span>) </span>
你当然可以。 使用 XPATH 的 contains 方法,结合 select 任何属性(@aria-label)的能力:
//a[contains(@aria-label, 'Bewertungspunktestand:')]
专门获取该链接元素的文本值:
trans = driver.find_element_by_xpath("//a[contains(@aria-label, 'Bewertungspunktestand:')]").text
aria-label属性的值不是Bewertungspunktestand
而是Bewertungspunktestand: 32
。
要从innerHTML
打印值 ie 32
,您可以使用以下任一Locator Strategies :
使用css_selector
和text属性:
driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true') print(driver.find_element_by_css_selector("a[aria-label^='Bewertungspunktestand']").text)
使用xpath
和get_attribute()
:
driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true') print(driver.find_element_by_xpath("//a[starts-with(@aria-label, 'Bewertungspunktestand')]").get_attribute("innerHTML"))
理想情况下,您需要为visibility_of_element_located()
引入WebDriverWait ,并且您可以使用以下任一Locator Strategies :
使用CSS_SELECTOR
和get_attribute()
:
driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true') print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[aria-label^='Bewertungspunktestand']"))).get_attribute("innerHTML"))
使用XPATH
和文本属性:
driver.get('https://www.ebay.de/itm/Apple-MacBook-Pro-15-Laptop-mit-Touchbar-512GB-MPTT2D-A-Wie-neu/133585540546?nordt=true&nma=true&orig_cvip=true') print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[starts-with(@aria-label, 'Bewertungspunktestand')]"))).text)
控制台 Output:
MyMercy User
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
链接到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性返回The text of the element.
从您的查询中,我了解到您想要获取页面中的所有 aria-label。 XPath 下面将返回网页上的所有 aria-label 值,您可以使用循环遍历这些值。
//span[@class='mbg-l']/a/@aria-label
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.