如何使用 css 选择器或任何其他定位器 selenium python 查找具有特定 class 属性的元素
how to find element with specific class attributes using css selector or any other locator selenium python
我正在尝试在同一 class 中查找使用属性的元素,下面是
的元素
div class="outcome-pricedecimal " data-markettypecategory="00000000-0000-0000-da7a-000000580001" data-pd="1.66">1.66 </div>
我想使用 data-pd="1.66" 定位它
它是我需要的十进制数,因为我希望它在小数点更改为我的首选值时找到元素(例如,当 =1.05 时查找数据-pd)
我尝试使用此代码找到它
driver.find elements_by_xpath("//div[contains(number()
driver.find_elements_by_css_selector('div.data-pd="1.05)"and not:(.outcome-pricedecimal) (.data-markettypecategory="00000000-0000-0000-da7a-000000580001)"]')[0].click()
driver.find_element_by_css_selector(div.1.01)
感谢您的回答和帮助...
为此只需使用 BeautifulSoup
。这是使用 BeautifulSoup
:
执行此操作的代码
from bs4 import BeautifulSoup
html = '<div class="outcome-pricedecimal " data-markettypecategory="00000000-0000-0000-da7a-000000580001" data-pd="1.66">1.66 </div>'
soup = BeautifulSoup(html,'html5lib')
div = soup.find('div')
data_pd = div['data-pd']
print(data_pd)
输出:
1.66
如果你还想用 selenium
做,那么你可以这样做:
data_pd = driver.find_element_by_class_name("outcome-pricedecimal ").get_attribute('data-pd')
print(data_pd)
我正在尝试在同一 class 中查找使用属性的元素,下面是
的元素div class="outcome-pricedecimal " data-markettypecategory="00000000-0000-0000-da7a-000000580001" data-pd="1.66">1.66 </div>
我想使用 data-pd="1.66" 定位它 它是我需要的十进制数,因为我希望它在小数点更改为我的首选值时找到元素(例如,当 =1.05 时查找数据-pd) 我尝试使用此代码找到它
driver.find elements_by_xpath("//div[contains(number()
driver.find_elements_by_css_selector('div.data-pd="1.05)"and not:(.outcome-pricedecimal) (.data-markettypecategory="00000000-0000-0000-da7a-000000580001)"]')[0].click()
driver.find_element_by_css_selector(div.1.01)
感谢您的回答和帮助...
为此只需使用 BeautifulSoup
。这是使用 BeautifulSoup
:
from bs4 import BeautifulSoup
html = '<div class="outcome-pricedecimal " data-markettypecategory="00000000-0000-0000-da7a-000000580001" data-pd="1.66">1.66 </div>'
soup = BeautifulSoup(html,'html5lib')
div = soup.find('div')
data_pd = div['data-pd']
print(data_pd)
输出:
1.66
如果你还想用 selenium
做,那么你可以这样做:
data_pd = driver.find_element_by_class_name("outcome-pricedecimal ").get_attribute('data-pd')
print(data_pd)