我是编程新手,所以我可能是一个简单的问题,但我不明白为什么 Selenium 打开的浏览器在代码末尾关闭。
from lib2to3.pgen2 import driver
from selenium import webdriver
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="063f333e746365726f684662637663626b67746f6d6f68672865696b" rel="noreferrer noopener nofollow">[email protected]</a>')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
最佳答案
您可以使用两种方法来保持驱动程序打开。
1.
将“分离”选项添加到您的驱动程序设置中:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
只需在测试代码末尾添加延迟(不太优雅的方法,但更简单)
from lib2to3.pgen2 import driver
from selenium import webdriver
import time
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92aba7aae0f7f1e6fbfcd2f6f7e2f7f6fff3e0fbf9fbfcf3bcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
time.sleep(50)
关于python - Selenium在完成所有功能后单独关闭浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70820115/