使用WebDriver和Selenium在类中获取span

2024-10-03 17:21:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在一个跨度内删除一个人的名字。 这个跨度在一个类中。在

遵循HTML:

<div>
<a class="_32mo" href="https://www.facebook.com/goutham.pullela?ref=br_rs">
<span>Goutham Pullela</span>
</a>
</div>

我尝试使用find_elements_by_class_name,然后得到attribute,但是span不是{}。我没有这个span的标识。在

^{pr2}$

Tags: httpsbrdivcomreffacebookhtmlwww
3条回答

一个更简单的方法是,检查元素,然后在控制台左上角,使用箭头标记并单击Goutham Pullela,然后您将看到它在控制台上高亮显示。复制xpath并使用name = browser.find_elements_by_xpath('[paste xpath']).textprint(name)

您只需使用CSS选择器,然后打印span元素的text属性:

contacts = browser.find_elements_by_css_selector('._32mo span')
for contact in contacts:
    print(contact.text)
# First we login into Facebook using:

from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('https://www.facebook.com/')
driver.implicitly_wait(10)
email = driver.find_element_by_id("email")
email.send_keys("email@domain.tld")
passwd = driver.find_element_by_id("pass")
passwd.send_keys("MyP@$$w0rd");
passwd.send_keys(Keys.ENTER)

# Then We can search and retrieve the name list:

driver.get('https://www.facebook.com/search/283544874786/likers?ref=about')
x = driver.find_elements_by_xpath(".//a[contains(@class, '_32mo')]")
for y in x:
    print(y.text)

^{pr2}$

相关问题 更多 >