Skip to content

Instantly share code, notes, and snippets.

@prakritidev
Created July 20, 2018 15:20
Show Gist options
  • Save prakritidev/569524b40ed87d174fcaeabbb4d11eb4 to your computer and use it in GitHub Desktop.
Save prakritidev/569524b40ed87d174fcaeabbb4d11eb4 to your computer and use it in GitHub Desktop.
Open link in new tab using selenium python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome("drivers/chromedriver")
try:
driver.get("http://www.google.com")
input_element = driver.find_element_by_name("q")
input_element.send_keys("fuck you")
input_element.submit()
link = driver.find_element_by_xpath(
'//*[@id="rso"]/div[1]/div/div[2]/div/div/h3/a').click()
# for windows change command to control.
ActionChains(driver).move_to_element(link).key_down(Keys.COMMAND).click(link).key_up(Keys.COMMAND).perform()
time.sleep(5)
except Exception as e:
print(e)
driver.close()
@Kieron-vB2
Copy link

thank you

@snapfast
Copy link

Does not work to open link in new tab. Windows 10, Python 3.10

@MohamedMesto
Copy link

Thanks Prakritidev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment