How can I open a new tab with selenium python? - python

I'm trying to make a program that opens multiple websites, but I can't get it to press control-t. I've tried multiple solutions, but I can't find one that works. When I do the keydown method, I get an error that says
webdriver has no attribute key_down
and when I try send_keys(Keys.CONTROL + 't') it doesn't raise any errors, or do anything. How can I open a new tab?
Here's my try :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://youtube.com")
search = driver.find_element_by_id("search")
#search.keydown(Keys.CONTROL)
#Webelement.key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
search.send_keys(Keys.CONTROL+'t')
time.sleep(10)

You can do it as
from selenium import webdriver
driver.get("https://www.youtube.com")
search = driver.find_element_by_id("search")
driver.execute_script("window.open('https://www.google.com')")

Here's what you can try :
from selenium import webdriver
webBrowser = webdriver.<>()
# This is first tab
webBrowser.get('<>')
# Second tab
webBrowser.execute_script("window.open('about:blank','secondtab');")
webBrowser.switch_to.window("secondtab")
webBrowser.get('<>')
# Third tab
webBrowser.execute_script("window.open('about:blank','thirdtab');")
webBrowser.switch_to.window("thirdtab")
webBrowser.get('<>')
You can learn more about it from Python – Opening multiple tabs using Selenium
Additionally I think you also have you look here.
However, if you want to run something on multiple windows, then I recommend having multiple instances of webdriver. It is much easier to manage, and is supported (There are workarounds on opening a new tab/window, such as pressing a hotkey that opens a new window, but they aren't supported).

You can use pyautogui to doy to open new tab using ctrl+t :
import pyautogui
pyautogui.keyDown('ctrl')
pyautogui.press('t')
pyautogui.keyUp('ctrl')

driver.switch_to.new_window()
From the webdriver docs
You can pass in 'tab' or 'window'. For ChromeDriver, it opens a tab by default.

Related

How to open a url using an adress of an application

How to open a URL without webbrowser using Python, but with an address of the application with which I want to open it.
There are three ways to do this
Case 1: You just need to open a page. and that is it,
import webbrowser
webbrowser.open("https://yoursite.com")
The above will open https://yoursite.com on your default browser
Case 2: You need to open, as well as do a task eg refresh
from selenium import webdriver
import time
driver = webdriver.Edge("path to your edge driver")
driver.get("https://yoursite.com")
# The command below will refresh your webbrowser after 3 seconds of opening it
time.sleep(3)
driver.refresh()
If you want to use chrome... use driver = webdriver.Chrome("path to your chrome driver")
Case 3:You have already opened your webbrowser (via selenium) and you are in another program and you want to see it without opening it manually
from selenium import webdriver
import time
driver = webdriver.Edge("path to your edge driver")
driver.get("https://yoursite.com")
time.sleep(10)
# this command stores your webbrowser window in a variable
main_page = driver.current_window_handle
# this command moves the webbrowser to the front
driver.switch_to.window(main_page)
Also you have to install a driver for your browser if you are using the selenium thing...
For edge go to https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#:~:text=Microsoft%20Edge%20WebDriver%20for%20Microsoft%20Edge%20will%20work,find%20your%20correct%20build%20number%3A%20Launch%20Microsoft%20Edge. and choose your edge version...
And for chrome go to https://chromedriver.chromium.org/downloads
You will also have to do pip install selenium in the terminal...
Hope this helps ;)
Try selenium, although you'll have to first download Chromedriver:
from selenium import webdriver from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox() driver.get("http://stackoverflow.com/")
body = driver.find_element_by_tag_name("body") body.send_keys(Keys.CONTROL + 't')
driver.close()

selenium firefox webdriver doesn't make a complete download of a PDF with python

I'm trying to download a PDF with python, from a java event not html, and I already changed my firefox preferences, it makes the download but when I try to open the file it says that the file must be damaged or corrupt, I noted that when firefox webdriver makes the download, it doesn't download all the bytes of it so I don't know if it doesn't wait till the download is complete or if there's something I'm missing in my code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/pdf")
fp.set_preference("pdfjs.disabled",True)
fp.set_preference("browser.download.dir", "C:\\Users\\carlo\\Desktop\\
driver = webdriver.Firefox(firefox_profile=fp)
driver.get(the url which i cant give here)
Then I open a new window in the code and take control of it which is a PDF web window, and use this:
element= WebDriverWait(driver, 10).\until(EC.visibility_of_element_located((By.XPATH,"//*[#id='download']")))
element.click()
Additional to this, it doesn't have a URL; it's a java event that doesn't come with that and now it starts the download but it corrupts it. I have tried to wait with time.sleep but it still has the same problem. If there's a way to set a preference of direct download without opening a new window with the driver it should help, am I missing something?
I've already figured it out just needed to add time in the element.click.sleep(2)
In addition, you can verify if the file has been downloaded before quiting the browser:
import glob
import time
download_dir = "C:\\Users\\carlo\\Desktop"
def still_downloading(download_dir):
files = glob.glob(download_dir+"/*.part")
if len(files) > 0:
return True
return False
...
element.click()
while still_downloading(dl_location):
print "still downloading..."
time.sleep(1)
This way you don't have to "guess" the time needed to download your file beforehand.

Send Ctrl+f python selenium chrome

I am able to use some keys like Tab
browser.find_element_by_tag_name('body').send_keys(Keys.TAB)
But not Ctrl+f or Ctrl+p
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL +'f')
I also tried using xpath find_element_by_xpath & send_keys(Keys.CONTROL,'f') but still not working
You shall try by using action_chains in selenium webdriver
from selenium.webdriver.common.action_chains import ActionChains
#
# Your code
#
browser.find_element_by_tag_name('body')
ActionChains(browser).send_keys(Keys.CONTROL, "f").perform()
To be honest I have not known the answer before. I searched and run code using selenium.webdriver.common.action_chains but failed.
Then after more research, I was suggested to use pyautogui library. I don’t know how, but it worked for me… you can try the code.
# Import necessary libraries
from selenium import webdriver
import pyautogui
# to get the broswer
driver = webdriver.Chrome(executable_path="C:\Windows\chromedriver_win32 (1)\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.google.com/")
# code to perform ctrl+F
pyautogui.hotkey('ctrl', 'f')

python-selenium and ChromeDriver: Can't open tab to control it

I'm unable to open a new tab - or even click on a link and open it on a new tab - with Python 2.7.1, Selenium 2.53.0, ChromeDriver 2.22 and Google Chrome 51.0 on Mac OS X.
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get('https://twitter.com')
driver.find_element_by_id('signin-email').send_keys(Keys.COMMAND + 't')
It opens the URL, but it doesn't open a new tab.
I've also tested opening a new tab with ActionChains, but no success.
The only method to open new tab that works is with JavaScript through execute_script(), but I'm not able to control the new tab in that case. switch_to.window() doesn't work.
You should try as below :-
driver.find_element_by_id('signin-email').send_keys(Keys.COMMAND + Keys.RETURN);
Hope it will help you..:)
You can try to click on the link while holding the ctrl key, this will open the link in new tab
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).perform()
driver.find_element_by_id('signin-email').click()
actions.key_up(Keys.CONTROL).perform()

Send keys control + click in Selenium with Python bindings

I need to open link in new tab using Selenium.
So is it possible to perform ctrl+click on element in Selenium to open it in new tab?
Use an ActionChain with key_down to press the control key, and key_up to release it:
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://google.com')
element = driver.find_element_by_link_text('About')
ActionChains(driver) \
.key_down(Keys.CONTROL) \
.click(element) \
.key_up(Keys.CONTROL) \
.perform()
time.sleep(10) # Pause to allow you to inspect the browser.
driver.quit()
Two possible solutions:
opening a new tab
self.driver = webdriver.Firefox()
self.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
this is the solution for MAC OSX. In other cases you can use the standard Keys.CONTROL + 't'
opening a new webdriver
driver = webdriver.Firefox() #1st window
second_driver = webdriver.Firefox() #2nd windows
Below is what i have tried for Selenium WebDriver with Java binding and its working for me.
If you want to manually open the Link in New Tab you can achieve this by performing Context Click on the Link and selecting 'Open in new Tab' option. Below is the implementation in Selenium web-driver with Java binding.
Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//xpath of the element"));
//Open the link in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Web-driver handles new tab in the same way to that of new window. You will have to switch to new open tab by its window name.
driver.switchTo().window(windowName);
You can keep track of window-names which will help you to easily navigate between tabs.
By importing Keys Class, we can open page in new tab or new window with CONTROL or SHIFT and ENTER these keys:
driver.find_element_by_xpath('//input[#name="login"]').send_keys(Keys.CONTROL,Keys.ENTER)
or
driver.find_element_by_xpath('//input[#name="login"]').send_keys(Keys.SHIFT,Keys.ENTER)
For python, a good solution would be driver.find_element_by_css_selector('<enter css selector here>').send_keys(Keys.CONTROL, Keys.RETURN)
After that, you should change windows so selenium could function in the new window
window_before = driver.window_handles[0]
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
After using the window is useful to close it or go back to the original:
Close tab: driver.close()
Back to the original: driver.switch_to_window(window_before)
Also try driver.switch_to.window() if your Selenium version does not support the other one
Following is working for me to open link in new tab :
String link = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.linkText("yourlinktext")).sendKeys(link);
Above code is in java. you can convert to python easily I assume.
Please ask if have any query.

Categories

Resources