有多个窗口可以获取信息
我有一个csv与关键字搜索在第一列,有时网站不包含元素,所以我希望司机退出,然后它可以发送下一个关键字。
这里我给出了一个我想要搜索的具体示例关键字(以及正常情况):
url = 'http://biz.finance.sina.com.cn/suggest/lookup_n.php?country=&q=21%BD%F0%BF%C602'
# the following is a normal situation with all elements exist
# url = 'http://money.finance.sina.com.cn/bond/quotes/sz149373.html'
driver.get(url)
try:
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_xpath('//div[@class="title tit06"]/a').click()
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_link_text('基本资料').click()
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_link_text('发行信息').click()
except:
try:
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_link_text('基本资料').click()
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_link_text('发行信息').click()
except:
#error after quitting, cannot continue to search next key
**driver.quit()**
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_link_text('基本资料').click()
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_link_text('发行信息').click()错误:MaxRetryError: HTTPConnectionPool(host='127.0.0.1',port=63963):超过url的最大重试次数:
当chrome在突出显示的行中退出时,错误如下所示。
非常感谢!
发布于 2021-02-25 18:36:06
driver.quit()将关闭您计算机中的所有驱动程序操作。你收到的错误意味着它能够找到你的驱动程序,因为它已经被你的代码关闭了。不能执行进一步交互。
如果要关闭当前网站,请在当前窗口中。切换回父窗口并改用driver.close() (请记住跟踪父窗口):
driver.switch_to.window(parent_window)
driver.close()它只关闭当前窗口,你的驱动程序正在关注你的情况示例on.Code:
for key in read_key_from_csv:
# declare your driver inside loop
driver = webdriver.Chrome()
driver.get(key)
# put all your code in side the loop
# in case it not find element you want close the driver, the loop will jump to next key and start new driver
driver.close()
# no action relate to driver can perform after you close ithttps://stackoverflow.com/questions/66366376
复制相似问题