简体   繁体   中英

How can I find specific but obscure elements in Selenium, without a parser like Beautiful Soup?

I am using Selenium to try and find a group of specific elements in a web page that look like this:

<br>Time remaining: <span style='color:red;font-weight:bold' > 4 mins, 40 secs</span>
<br>Time remaining: <span style='color:red;font-weight:bold' > 12 mins</span>
<br>Time remaining: <span  > 5 hrs, 39 mins</span>

There are usually around 10 to 20 of these and before I realized that some weren't red, I just used this:

timeleft = browser.find_elements_by_css_selector("span[style='color:red;font-weight:bold']")

The text of the element can vary between "mins, secs" , "mins" , "secs" , "hrs , mins" etc. I am trying to find a way to get those elements without the "style" .

This should work for you, unless the time words show up in other spans on the page that you don't want included...

browser.find_elements_by_xpath(
    "//span[contains(text(), 'secs') or contains(text(), 'mins') or contains(text(), 'hrs')]")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM