admin管理员组文章数量:1128329
In my python code I initialize selenium chrome webdriver with certain parameters, open a web page, click a button which does open a new window if I comment chrome_options.add_argument("--headless")
line. Then I switch to driver.window_handles[-1]
.
If I run code without chrome_options.add_argument("--headless")
line, driver.window_handles
returns 2 windows, if I run in headless mode, driver.window_handles
returns 1 window only. I have tried adding delay up to 300 seconds, it does not help. Then code crashes because driver can not find button from an unopened window. Take a look at the code:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument(f"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0")
chrome_options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 30)
def main():
driver.get("...")
time.sleep(60) # no matter how long is the delay, the outcome doesn`t change
button = wait.until(ec.element_to_be_clickable((By.XPATH, "...")))
button.click()
# after button above is clicked, a new window must be opened
time.sleep(60)
print(f"WINDOW HANDLES: {driver.window_handles}")
# for headless chrome prints WINDOW HANDLES: ['24931185E13B5F865C2AF60824C63581']
# normally prints WINDOW HANDLES: ['2C26F32DB25AF6BA3385E57788A9A0F0', '573A622D02011B22274612B2AA81C8DC']
driver.switch_to.window(driver.window_handles[-1])
button = wait.until(ec.element_to_be_clickable((By.XPATH, "...")))
button.click()
# in headless mode raises exception as new window where button is to be found is not opened
)
How to open a new window triggered by button click and switch to it while running selenium in headless mode?
In my python code I initialize selenium chrome webdriver with certain parameters, open a web page, click a button which does open a new window if I comment chrome_options.add_argument("--headless")
line. Then I switch to driver.window_handles[-1]
.
If I run code without chrome_options.add_argument("--headless")
line, driver.window_handles
returns 2 windows, if I run in headless mode, driver.window_handles
returns 1 window only. I have tried adding delay up to 300 seconds, it does not help. Then code crashes because driver can not find button from an unopened window. Take a look at the code:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument(f"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0")
chrome_options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 30)
def main():
driver.get("...")
time.sleep(60) # no matter how long is the delay, the outcome doesn`t change
button = wait.until(ec.element_to_be_clickable((By.XPATH, "...")))
button.click()
# after button above is clicked, a new window must be opened
time.sleep(60)
print(f"WINDOW HANDLES: {driver.window_handles}")
# for headless chrome prints WINDOW HANDLES: ['24931185E13B5F865C2AF60824C63581']
# normally prints WINDOW HANDLES: ['2C26F32DB25AF6BA3385E57788A9A0F0', '573A622D02011B22274612B2AA81C8DC']
driver.switch_to.window(driver.window_handles[-1])
button = wait.until(ec.element_to_be_clickable((By.XPATH, "...")))
button.click()
# in headless mode raises exception as new window where button is to be found is not opened
)
How to open a new window triggered by button click and switch to it while running selenium in headless mode?
Share Improve this question edited Jan 8 at 15:25 tegularis asked Jan 8 at 14:27 tegularistegularis 6910 bronze badges1 Answer
Reset to default 0You can use the new --headless=new
flag instad of --headless
.
The refactored options could look like the following:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless=new")
chrome_options.add_argument("--disable-gpu")
# chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument(f"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0")
driver = webdriver.Chrome(options=chrome_options)
本文标签: google chromePython Selenium Headless Webdrivers windowhandles doesnt see new windowsStack Overflow
版权声明:本文标题:google chrome - Python Selenium Headless Webdriver`s window_handles doesn`t see new windows - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736718102a1949313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论