【发布时间】:2017-11-04 11:38:31
【问题描述】:
我正在尝试获取 div 中的 html/文本。 div 有一个 class 数学。
这是我使用的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from bs4 import BeautifulSoup as soup
from bs4 import SoupStrainer
import urllib.request
from selenium.webdriver.common.action_chains import ActionChains
import getpass
ui = input('What is your IXL username?\n\n')
pi = getpass.getpass('\nWhat is your IXL password?\n\n')
driver = 'C:\\Users\\agzsc\\Desktop\\MicrosoftWebDriver.exe'
driver = webdriver.Edge(driver)
driver.get('https://www.ixl.com')
username = driver.find_element_by_id('qlusername')
password = driver.find_element_by_id('qlpassword')
submit = driver.find_element_by_id('qlsubmit')
username.send_keys(ui)
password.send_keys(pi)
ActionChains(driver).move_to_element(submit).click().perform()
for x in range(1):
time.sleep(1)
driver.execute_script('''window.open("https://www.ixl.com/math/grade-3/multiply-by-11","_blank");''')
driver.switch_to_window(driver.window_handles[1+x])
math = soup.find_all('div', attrs={"class":"math"})
print(math)
如您所见,我正在为 Microsoft Edge 使用 selenium webdriver。我也试图用bs4 解析页面,并且只用class 数学得到div。但是,我不断收到此错误:
Traceback (most recent call last):
File "C:\Users\agzsc\Downloads\powerixl.py", line 41, in <module>
math = soup.find_all('div', attrs={"class":"math"})
File "C:\Users\agzsc\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\element.py", line 1310, in find_all
generator = self.descendants
AttributeError: 'str' object has no attribute 'descendants'
如果有人可以提供帮助,我将非常亲切。谢谢!
【问题讨论】:
标签: python selenium webdriver bs4