我想保持Chrome打开并关闭控制台。我已经读过webdriver.service.stop()可以做到这一点,但我无法使其正常工作。我试过从selenium导入几项东西,它们是namec服务,但我仍然无法得到selenium.webdriver没有属性'service'。最后两个导入未使用。当前,当我运行不带pycharmt的脚本时,控制台会启动,如果在运行后将其关闭,它也会关闭chrome窗口。我不想手动关闭控制台窗口。
这是我截至2019-03-15的更新代码:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common import service
from selenium.webdriver.ie.service import service
chrome_options = ChromeOptions()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--start-maximized")
chrome_options.set_capability("detach", True)
DynamoForum = webdriver.Chrome(chrome_options=chrome_options)
DynamoForum.get("https://forum.dynamobim.com/")
Parent_window = DynamoForum.window_handles[0]
login = DynamoForum.find_element_by_class_name("header-buttons").click()
wait = WebDriverWait(DynamoForum, 20)
window_child = DynamoForum.window_handles[1]
DynamoForum.switch_to.window(window_child)
wait.until(EC.visibility_of_element_located((By.ID, "userName"))).send_keys("abc@abc.com")
DynamoForum.find_element_by_id("verify_user_btn").click()
wait.until(EC.visibility_of_element_located((By.ID, "password"))).send_keys("abc")
DynamoForum.find_element_by_id("btnSubmit").click()
try:
DynamoForum.service.stop()
except AttributeError:
pass
下面是旧代码:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common import service
from selenium.webdriver.ie.service import service
chrome_options = ChromeOptions()
chrome_options.add_argument("--incognito")
DynamoForum = webdriver.Chrome(chrome_options=chrome_options)
DynamoForum.get("https://forum.dynamobim.com/")
Parent_window = DynamoForum.window_handles[0]
login = DynamoForum.find_element_by_class_name("header-buttons").click()
wait = WebDriverWait(DynamoForum, 20)
window_child = DynamoForum.window_handles[1]
DynamoForum.switch_to.window(window_child)
wait.until(EC.visibility_of_element_located((By.ID,
"userName"))).send_keys("abc@abc.com")
DynamoForum.find_element_by_id("verify_user_btn").click()
wait.until(EC.visibility_of_element_located((By.ID,
"password"))).send_keys("abc")
DynamoForum.find_element_by_id("btnSubmit").click()
webdriver.service.stop()
这是我遇到的错误。通过将webdriver.serive.stop更改为DynamoForum.service.stop,可以解决此问题。
C:/Users/cjr/PycharmProjects/DynamoForum/DynamoForum.py:13:
DeprecationWarning: use options instead of chrome_options
DynamoForum = webdriver.Chrome(chrome_options=chrome_options)
Traceback (most recent call last):
File "C:/Users/cjr/PycharmProjects/DynamoForum/DynamoForum.py", line 29,
in <module>
webdriver.service.stop()
AttributeError: module 'selenium.webdriver' has no attribute 'service'
答案 0 :(得分:0)
这仅是解决方法 我知道这不是正确的方法,但是暂时您可以用下面的内容代替最后一行。这将通过执行,但是在执行后保持浏览器打开。
try:
webdriver.service.stop()
except AttributeError:
pass
让我知道这是否有帮助。同时将继续对此进行挖掘。