Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Tab使用Selenium和BeautifulSoup时卡住_Python_Selenium_Webdriver_Beautifulsoup - Fatal编程技术网

Python:Tab使用Selenium和BeautifulSoup时卡住

Python:Tab使用Selenium和BeautifulSoup时卡住,python,selenium,webdriver,beautifulsoup,Python,Selenium,Webdriver,Beautifulsoup,我正在尝试使用selenium和BeautifulSoup获取几个链接的源代码。我打开第一个选项卡以获取工作正常的源代码,但是第二个选项卡卡住了。我想这是因为美苏的缘故。有人知道为什么吗?有人知道BeautifulSoup的替代方案吗?代码如下: from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from bs4 import Beauti

我正在尝试使用selenium和BeautifulSoup获取几个链接的源代码。我打开第一个选项卡以获取工作正常的源代码,但是第二个选项卡卡住了。我想这是因为美苏的缘故。有人知道为什么吗?有人知道BeautifulSoup的替代方案吗?代码如下:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup

links = []
driver = webdriver.Firefox()
driver.get('about:blank')
for link in links:          
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
    browser.get(link)       
    source = str(BeautifulSoup(browser.page_source))            
    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')
driver.close()

在您的示例中,BeautifulSoup的功能是什么?browser.page\u源已经是字符串。。。这是假设在这两种情况下,浏览器都是驱动程序的输入错误。这是您的确切代码还是您重新键入的?您需要更改selenium激活的活动窗口。driver.switch_to_windowdriver.window_句柄[-1]将切换到最后一个选项卡,而driver.switch_to_windowdriver.window_句柄[1]将切换到第二个选项卡,以此类推。browser.close也将关闭当前窗口,Ctrl+w似乎有点老套。