[英]BeautifulSoup: How to access text in nested Span
I am new to Selenium, and I want to get the string "United States" from a website , HTML as shown below:我是 Selenium 的新手,我想从网站HTML 获取字符串“United States”,如下所示:
I am confused as to how to access the span for country, as there is another span with the exact same class shown below.我对如何访问国家/地区的跨度感到困惑,因为还有另一个跨度与下面显示的完全相同的 class 。
My code:我的代码:
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
CLASS_NAME = "class name"
if (driver.find_elements(By.CLASS_NAME, "css-1gzpoyq e1wnkr790")!=None):
print(driver.find_elements(By.CLASS_NAME, "css-1gzpoyq e1wnkr790").text)
The output printed is AttributeError: 'list' object has no attribute 'text'
, and I'm unsure why that is so.打印的 output 是
AttributeError: 'list' object has no attribute 'text'
,我不确定为什么会这样。
I want to get the string "United States"
我想得到字符串“美国”
Simply adjust your CSS SELECTOR
use .find_element
to select only one / the first element.只需将您的
CSS SELECTOR
使用.find_element
调整为 select 只有一个/第一个元素。
Following selector goes for for sibling <div>
of elemetn with id="cmp-Select-Location-label"
and selects the <span>
by its attribute cmp-Select-Location-label
:以下选择器用于
id="cmp-Select-Location-label"
的 elemetn 的兄弟<div>
并通过其属性cmp-Select-Location-label
选择<span>
:
...
url = 'https://www.indeed.com/cmp/Quintal-Contracting/reviews'
driver.get(url)
driver.find_element(By.CSS_SELECTOR, '#cmp-Select-Location-label + div [data-testid="selected-value"]').text
You may think it looks a bit complecated but I recommend to select by more static information like identifiers or HTML structure instead of dynamic classes您可能认为它看起来有点复杂,但我建议 select 使用更多 static 信息,例如标识符或 HTML 结构而不是动态类
Output: Output:
United States
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.