pythonwebform菜单窗口动画在哪,Selenium / Python将Webdriver从无头模式切换到窗口模式...

Is there any way to switch Chrome webdriver from headless mode to window mode?

One thing that came to my mind is to 'switch' existing web driver to non-headless mode.

Another idea: to create new instance of webdriver (this time non-headless) with some sort of 'state' from old one so the user operations can be executed. I don't know how to do or if it is possible though.

import os

from selenium import webdriver

from selenium.common.exceptions import NoSuchElementException,

options = webdriver.ChromeOptions()

options.add_argument('headless')

driver = webdriver.Chrome(

executable_path=os.path.join(os.getcwd(), 'chromedriver'),

chrome_options=options,

)

driver.get('https://website.com')

try:

driver.find_element_by_xpath('//h1').click()

except NoSuchElementException:

print('You have to click it manually')

# here I need Chrome browser

# to be opened so that I can click a link

print('The name of this thing is: ', end='')

print(driver.find_element_by_xpath("//h1[@class='name']").text)

解决方案

If you need to open a new tab

driver.execute_script("window.open()")

If you need to switch to this new one

driver.switch_to.window(self.driver.window_handles[1])

Then you get the page

driver.get('https://website.com')

and the end you can close it (the new one)

driver.close()

and you back to the first driver

switch_to.window(driver.window_handles[0])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值