我正在尝试运行selenium web scrape但它不会打印我想要的数据。页面将加载但终端中没有打印。终端只是说“DevTools听ws ......”
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time
url = "https://www.tdameritrade.com/home.page"
username = "XXXXX"
password = "XXXXX"
browser = webdriver.Chrome()
if __name__ == "__main__":
browser.get(url)
uname = browser.find_element_by_name("tbUsername")
uname.send_keys(username)
passw = browser.find_element_by_name("tbPassword")
passw.send_keys(password)
submit_button = browser.find_element_by_xpath("//div[@class='main-header-login-fields']//button[@class='main-header-login-submit main-header-login-label btn btn-green-solid']").click()
time.sleep(10)#delay to answer extra login/security questions
browser.get("https://invest.ameritrade.com/grid/p/site#r=jPage/https://research.ameritrade.com/grid/wwws/research/Markets/SectorsIndustries/Industry?symbol=220992436&c_name=invest_VENDOR")
cell = browser.find_element_by_xpath("""//*[@id="module-detail"]/div/div/div[1]/div/div[3]/div[2]/span""")
print cell.text
编辑:
我不确定它失败的地方。我将最后几行更改为下面的代码,但终端仍然只停留在“DevTools listen ...”页面加载但不打印错误。只有当我关闭浏览器时,它才会到达并打印“已完成”。
browser.get("https://invest.ameritrade.com/grid/p/site#r=jPage/https://research.ameritrade.com/grid/wwws/research/Markets/SectorsIndustries/Industry?symbol=220992436&c_name=invest_VENDOR")
try:
browser.switch_to.window(browser.current_window_handle)
cell = browser.find_element_by_xpath("""//*[@id="module-detail"]/div/div/div[1]/div/div[3]/div[2]/span""")
except Exception as e:
print e
pass
print "finished"
答案 0 :(得分:0)
试试这个:
之前:
cell = browser.find_element_by_xpath("""//*[@id="module-detail"]/div/div/div[1]/div/div[3]/div[2]/span""")
行添加
browser.switch_to.window(browser.current_window_handle)
然后再试一次。希望这会对你有所帮助! :)