简体   繁体   English

如何使用 css 选择器或任何其他定位器 selenium python 查找具有特定类属性的元素

[英]how to find element with specific class attributes using css selector or any other locator selenium python

Am trying to find an element using attribute in the same class,below is the element of the我正在尝试使用同一类中的属性查找元素,下面是

div class="outcome-pricedecimal " data-markettypecategory="00000000-0000-0000-da7a-000000580001" data-pd="1.66">1.66 </div>

I want to locate it using the data-pd="1.66" it the decimal number i need, because i want it to find the element if the decimal has changed to my prefered value(eg finding data-pd when =1.05) I tried finding it using this codes我想使用 data-pd="1.66" 它是我需要的十进制数来定位它,因为我希望它找到元素,如果十进制已更改为我的首选值(例如,当 =1.05 时查找 data-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_elements_by_css_selector('div.data-pd="1.05)" 而不是:(.outcome-priceecimal) (.data-markettypecategory="00000000-0000-0000-da7a-000000580001)"]')[0]。 ()

driver.find_element_by_css_selector(div.1.01) driver.find_element_by_css_selector(div.1.01)

thanks as you answer and help...感谢您的回答和帮助...

Just use BeautifulSoup for this.只需使用BeautifulSoup Here is the code to do it using 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)

Output:输出:

1.66

If you still wanna do it with selenium , then you can do it like this:如果你仍然想用selenium来做,那么你可以这样做:

data_pd = driver.find_element_by_class_name("outcome-pricedecimal ").get_attribute('data-pd')

print(data_pd)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在CSS上将CSS选择器与Selenium结合使用时找不到任何元素 - Can't find any element using CSS selector with Selenium on Python 在Python中使用Selenium无法通过任何定位器找到元素 - Can't find element by through any locator using Selenium in Python 如何在Selenium(Python)中查找具有特定类属性的元素? - How to find element with specific class attributes in selenium (python)? 如何在Python Selenium中使用xpath或CSS选择器查找元素 - How to find element using xpath or css selector in Python Selenium 如何使用python /硒通过CSS选择器查找元素 - how to find element by css selector using python / selenium 如何使用 Xpath、css 或 Selenium Python 中的任何其他定位器在 html 中的结束标记后查找带有“== $0”的元素? - How to find element with "== $0" after end tag in html using Xpath, css or any other locators in Selenium Python? Selenium Python:find_element_by_css_selector()使用:contains() - Selenium Python: find_element_by_css_selector() using :contains() 如何通过css选择器为Python硒中的日期字段查找元素? - How to find element by css selector for date field in Python selenium? Python selenium 通过 xpath 或 css 选择器查找元素 - Python selenium find element by xpath or css selector Selenium Python 通过 CSS 选择器查找元素 - Selenium Python find element by CSS Selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM