简体   繁体   English

没有像Beautiful Soup这样的解析器,如何在Selenium中找到特定但晦涩的元素?

[英]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: 我正在使用Selenium尝试在网页中查找如下所示的一组特定元素:

<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: 通常这些大约有10到20,在我意识到其中一些不是红色之前,我就使用了这个:

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" . 元素的文本可以在“ mins,secs”,“ mins”,“ secs”,“ hrs,mins”等之间变化。我试图找到一种方法来获取没有“ 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')]")

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

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