jac*_*ace 5 python selenium xpath css-selectors webdriverwait
我正在尝试获取锚标记内的信息,但不获取href
. 我想提取 eBay 上一些卖家的评分。在下面的 HTML 代码中,您可以看到评级分数的位置。有没有办法在不使用的情况下获取有关“Bewertungspunktestand”(德语评分)的信息href
,因为href
卖家之间的变化?本例中的评分为 32。由于文本“Bewertungspunktestand”仅在这一行中,我认为可以让它搜索该文本并提取包含该文本的 aria-label。
这是此示例的链接。
\n这是我尝试过的 Python 代码,但没有成功:
\ntry: \n trans = driver.find_element_by_xpath("//a[@aria-label=\'Bewertungspunktestand\']")\nexcept:\n trans = \'0\'\n
Run Code Online (Sandbox Code Playgroud)\n这是 HTML 代码
\n<span class="mbg-l">\n (<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>\n <span class="vi-mbgds3-bkImg vi-mbgds3-fb10-49" aria-label="Gelber Stern f\xc3\xbcr 10 bis 49 Bewertungspunkte" role="img"></span>)\n</span>\n
Run Code Online (Sandbox Code Playgroud)\n
DMa*_*art 14
你当然可以。使用 XPATH 的 contains 方法,结合选择任何属性的能力 (@aria-label):
//a[contains(@aria-label, 'Bewertungspunktestand:')]
Run Code Online (Sandbox Code Playgroud)
具体来说,要获取该链接元素的文本值:
trans = driver.find_element_by_xpath("//a[contains(@aria-label, 'Bewertungspunktestand:')]").text
Run Code Online (Sandbox Code Playgroud)
aria-label属性的值不是Bewertungspunktestand
but Bewertungspunktestand: 32
。
要打印值,32
即innerHTML
您可以使用以下定位器策略之一:
使用css_selector
和文本属性:
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)
Run Code Online (Sandbox Code Playgroud)
使用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"))
Run Code Online (Sandbox Code Playgroud)
理想情况下,您需要引发WebDriverWaitvisibility_of_element_located()
,并且可以使用以下任一定位器策略:
使用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"))
Run Code Online (Sandbox Code Playgroud)
使用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)
Run Code Online (Sandbox Code Playgroud)
控制台输出:
MyMercy User
Run Code Online (Sandbox Code Playgroud)
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Run Code Online (Sandbox Code Playgroud)
有用文档的链接:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性返回The text of the element.
归档时间: |
|
查看次数: |
23318 次 |
最近记录: |