browser.get('https://www.example.com')
browser.execute_script("arguments[0].click(), 'new_window'", browser.find_element_by_xpath('//*[@id="submitButton"]'))
此代码在某些网站上打开一个新标签。对于其他人,它无需打开新标签即可单击。
如何使它始终打开新标签页?
更新
browser.execute_script('window.open(arguments[0].click());', browser.find_element_by_xpath('//*[@id="submitButton"]'))
打开一个新选项卡,但在原始窗口中执行单击。将新标签页留空
答案 0 :(得分:2)
'window.open(arguments[0].click());'
只是进行了两项实际上不相关的操作(打开新标签页和单击按钮)
您可以尝试执行以下操作,以在单击“提交”按钮时强制打开新标签页:
input_button = browser.find_element_by_xpath('//*[@id="submitButton"]')
form = input_button.find_element_by_xpath('./ancestor::form')
browser.execute_script('arguments[0].target="_blank";', form)
input_button.click()