简体   繁体   中英

how to open a new window with selenium python

I am doing automation on a website.

I do a search on this site and your result returns a link for me to access, click it to open a new tab, but I want to open a new window

this is my code to click the link

WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a'))).click()

I tried using the SHIFT keyboard shortcut

WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a'))).send_keys(Keys.SHIFT).click()

but I have a result error

AttributeError: 'NoneType' object has no attribute 'click'

Is there a way I can configure chrome so every time I click a link it opens a new window?

from selenium.webdriver.chrome.options import Options

I think you have to write it like this by sending Shift and Enter :

WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a'))).send_keys(Keys.SHIFT,Keys.ENTER)

send_keys() as no return value, so you are getting None . You can do it with ActionChains

from selenium.webdriver import ActionChains

link = WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a')))
ActionChains(driver).key_down(Keys.SHIFT, link).click().key_up(Keys.SHIFT).perform()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM