如何使用Selenium和BeautifulSoup从标签中获取文本

时间:2019-01-10 21:39:05

标签: python selenium beautifulsoup

我需要获取验证码的文字说明。但是我无法通过BeautifulSoup获得它。请帮助修复它。

当我运行代码时:结果为“无”。

11.0.1

enter image description here

1 个答案:

答案 0 :(得分:1)

您要查找的内容位于不同的iframe上,您需要另外切换框架

.....
time.sleep(5)
# go to parent or top frame
driver.switch_to.default_content()
iframe = driver.find_element_by_css_selector('iframe[title="recaptcha challenge"]')
driver.switch_to.frame(iframe)
#title = driver.find_element_by_css_selector('.rc-imageselect-desc-wrapper strong') # cars
title = driver.find_element_by_class_name('rc-imageselect-desc-wrapper') # Select all images with cars.....
print(title.text)

# with BeautifulSoup
#html = driver.page_source()
#soup = BeautifulSoup(html, 'html.parser')
#title = soup.find('div',{'class':'rc-imageselect-desc-wrapper'})
#print (title)
相关问题