简体   繁体   English

使用 python 通过 webdriver 打开浏览器后代码停止执行

[英]Code stops executing after opening browser through webdriver using python

I have been trying to open multiple browser windows in internet explorer using webdriver in selenium.我一直在尝试使用 selenium 中的 webdriver 在 Internet Explorer 中打开多个浏览器 windows。 Once it reaches the get(url) line, it just halts there and eventually times out.一旦它到达 get(url) 行,它就会停在那里并最终超时。 I've added a print line, which does not execute.我添加了一条不执行的打印行。 I've tried various methods and the one below is the Ie version of code I used to open multiple tabs in Chrome.我尝试了各种方法,下面的一个是我用来在 Chrome 中打开多个标签的代码的 Ie 版本。 Even if I remove the first 3 lines, it still only goes up to opening google.com.即使我删除了前 3 行,它仍然只能打开 google.com。 I've looked googled this issue and looked through other posts but nothing has helped.我已经用谷歌搜索了这个问题并查看了其他帖子,但没有任何帮助。 Would really appreciate any advice, thanks!非常感谢任何建议,谢谢!

options = webdriver.IeOptions()
options.add_additional_option("detach", True)
driver = webdriver.Ie(options = options, executable_path=r'blahblah\IEDriverServer.exe')
driver.get("http://google.com")
print("syrfgf")
driver.execute_script("window.open('about:blank', 'tab2');")
driver.switch_to.window("tab2")
driver.get("http://yahoo.com")

You need to replace the url you have provided:您需要更换您提供的url

http://google.com

with a proper url as follows:使用正确的url如下:

https://www.google.com/

Which should be represented as per the syntax diagram as follows:应根据语法图表示如下:

网址语法

from selenium import webdriver
from selenium.webdriver.common.by import By
import time



driver = webdriver.Chrome()
driver.get("https://github.com")
    
signin_link = driver.find_element(By.LINK_TEXT, "Sign in")
signin_link.click()
time.sleep(1)

user = driver.find_element(By.ID, "login_field")
user.send_keys("X")

passw = driver.find_element(By.ID, "password")
passw.send_keys("X")

passw.submit()

time.sleep(5)

driver.close()

I had this issue and writing this code seems to have made it work flawlessly.我遇到了这个问题,编写这段代码似乎让它完美地工作。 Adjust the sleep time as you want it.根据需要调整睡眠时间。 Putting my chromedriver.exe into my project folder also helped with some errors将我的 chromedriver.exe 放入我的项目文件夹也有助于解决一些错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM