我正在尝试选择包含以下内容的所有 idcoupon-link
关键字与以下脚本。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://udemycoupon.discountsglobal.com/coupon-category/free-2/")
elems = driver.find_elements_by_css_selector('[id~=\"coupon-link\"]')
print(elems)
但我得到了一个空列表[]
作为结果。我的 css_selector 有什么问题吗?
我已经测试过了find_elements_by_css_selector('[id=\"coupon-link-92654\"]')
工作成功。但我想选择所有优惠券链接,而不仅仅是其中之一。
我参考了该文档w3schools.com http://www.w3schools.com/cssref/css_selectors.asp.
Selenium CSS 仅支持三个部分匹配运算符,即 -$^*
.
CSS 部分匹配表达式不正确 - 使用*
or ^
详细信息在here http://seleniumtestautomationtipsandtricks.blogspot.com/2013/01/find-elements-with-partial-match-on.html and here http://saucelabs.com/resources/selenium/css-selectors。您可以使用xpath
too.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://udemycoupon.discountsglobal.com/coupon-category/free-2/")
#select by css
#try *
css_lnks = [i.get_attribute('href') for i in driver.find_elements_by_css_selector('[id*=coupon-link]')]
#or try ^
#css_lnks = [i.get_attribute('href') for i in driver.find_elements_by_css_selector('[id^=coupon-link]')]
#select by xpath
xpth_lnks = [i.get_attribute('href') for i in driver.find_elements_by_xpath("//a[contains(@id,'coupon-link-')]")]
print xpth_lnks
print css_lnks
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)