I just installed Selenium Web Driver and tried it out. It works great. My use case can be describe as followed:
Start Firefox on a server with pseudo X server (Xvfb)
New Driver.Firefox() object
Open 10 tabs and load a webpage in each tab
Retrieve the html from all loaded pages
The only step that is not working is step 3. I can not find out how to open new tabs. I found this here on SO : How to open a new tab using Selenium WebDriver with Java? However, I tested this locally (i.e. with visible display) on my Mac for debugging purpose and I saw that the Firefox browser (which was opened when creating the driver object) does not open any tabs when doing as described on the SO thread. So I tried this here:
driver = webdriver.Firefox()
driver.get("https://stackoverflow.com/")
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
As I said, it does not work for me. So, how else is it possible to open tabs? I use Selenium 2.39 (pip install selenium) and Python 2.7.
the key combination to open a new tab on OSX is Command+T, so you should use
body.send_keys(Keys.COMMAND + 't')
It's probably slightly more correct to send it to the browser via action chaining since you're not actually typing text; this also makes your code more readable imo
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
# before correction from DMfll:
# ActionChains(driver).send_keys(Keys.COMMAND, "t").perform()
# correct method
ActionChains(driver).key_down(Keys.COMMAND).send_keys("t").key_up(Keys.COMMAND).perform()
Related
I have a webpage that just work properly on IE, In other browsers I get a messed up layout, but I need to automate it with headless mode, can selenium work on chrome even with the layout messed?
Does this webpage work fine in Chrome? If this is the case, you can implement this requriement via chrome driver with Selenium.
But if this webpage only work correctly in IE, the you can only implement automation in IE or Edge IE mode with Selenium. And Edge IE mode tabs actually running Internet Explorer, so they all use IE driver.
Here is a simple example:
from selenium import webdriver
from selenium.webdriver.ie.service import Service
ieOptions = webdriver.IeOptions()
ieOptions.add_additional_option("ie.edgechromium", True)
ieOptions.add_additional_option("ie.edgepath",'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
ser = Service(r"C:\Users\Administrator\Desktop\IEDriverServer.exe")
driver = webdriver.Ie(service = ser, options=ieOptions)
driver.maximize_window()
driver.get('https://www.google.com/')
Note: Please modify the path parameters according to your own situation.
Based on what I've read in stackoverflow, it seems that I need to send keys to open tab and then open a particular website. However, it's quite complicated to do so and I ended up using multiple webdrivers because it's way simpler.
Here's my dummy code:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver1 = webdriver.Chrome(executable_path=dirPath + "/chromedriver" + str(1),
options = chrome_options
driver2 = webdriver.Chrome(executable_path=dirPath + "/chromedriver" + str(2),
options = chrome_options
driver1.get("website1")
driver2.get("website2")
This then can be parallelized using multiprocessing. However it seems that this consumes a lot of RAM especially if I parallelize 10 webdrivers, each for one window. I was thinking of opening 10 tabs in 1 browser and then access each tab separately but got no solution. The goal is to open multiple tabs in one browser and use something like driver.get() in that tab without using keys.
Additional info: this setup crashed with seg fault 11 on pycharm but works fine in terminal
There's no built-in method for Selenium to open a new tab. However you can pass a keyboard shortcut like Ctrl + T to open a new tab
driver.find_element_by_css_name('body').send_keys(Keys.CONTROL + "T")
driver.switch_to.window(driver.window_handles[-1]) # This will switch the Selenium control to the current tab
But even this probably won't work in Chrome. I recommend you use some GUI automation tool like pyautogui to do it:
pyautogui.hotkey('ctrl', 't') # This will open a new tab
driver.switch_to.window(driver.window_handles[-1])
You can try both.
I am trying to open a list of different URLs, opening one per tab, but when the number exceeds 20 ~ 21, stop opening tabs.
I've tried to separate the list into groups of 20, and creating new instances of the webdriver, and that works fine, but I would like to know if it's a way to enable more number of tabs using the same instance?
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('https://stackoverflow.com/')
for i in range(30):
driver.execute_script("window.open('');")
print(len(driver.window_handles))
time.sleep(3)
driver.quit()
I was trying, to open 30 tabs at once but only opens 21.
I'm using python 3.5.0, Firefox 68.0.2 & geckodriver 0.24.0
If you look at the stackoverflow tab, you should see a yellow bar saying the rest has been blocked by the pop-up blocker. (This happens because execute_script runs the script in the context of the web page.)
To override, set dom.popup_maximum preference to a larger value:
opts = webdriver.FirefoxOptions()
opts.set_preference("dom.popup_maximum", 50)
driver = webdriver.Firefox(options=opts)
Please don't make use of "window.open()" to get new tabs or windows opened. Instead use the new WebDriver New Window API, which all the latest versions of the official Selenium bindings have been already integrated. Note, it's not part of all the drivers yet, but for recent Firefox releases it works.
Given that you are using the Python bindings the following can be used:
driver.switch_to.new_window('tab')
By using this approach there shouldn't be a limitation for opening a lot of tabs.
I searched for this question,and I found an idea using driver.switch_to.window(),but it didn't work as expect:
from selenium import webdriver
driver1=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver1.get('https://www.google.com')
driver2=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver2.get('https://www.bing.com/')
driver1.switch_to.window(driver1.current_window_handle)
above code will first open a chrome window and go to google,then will open another chrome window and go to bing,then
driver1.switch_to.window(driver1.current_window_handle)
seems didn't work,the window showing bing still shows on top of the window showing google.
Anyone have any idea?I think
driver1.switch_to.window(driver1.current_window_handle)
may have some BUG.
As you have used two WebDriver instances as driver1 and driver2 respectively to openthe urls https://www.google.com (e.g. windowA) and https://www.bing.com/ (e.g. windowB) it is worth to mention that the function switch_to.window() is a WebDriver method. So, driver1 can control only windowA and driver2 can control only windowB.
For Selenium to interact with any of the Browsing Window, Selenium needs focus. So to iterate among the different Browsing Windows you can shift the focus to the different Browsing Window using JavascriptExecutor as follows :
Python:
driver1.execute_script("window.focus();")
driver2.execute_script("window.focus();")
Java:
((JavascriptExecutor) driver1).executeScript("window.focus();");
((JavascriptExecutor) driver2).executeScript("window.focus();");
I believe you have a different concept of "window" in driver.switch_to.window(). In chrome browser, it means "tab". It's not another chrome browser or browser window like what are you trying to do in your code.
If switch_to.window() what you really want, I'll give an example how to use it:
driver=webdriver.Chrome("D:\Python\Files\chromedriver.exe")
driver.get('https://www.google.com')
# open a new tab with js
driver.execute_script("window.open('https://www.bing.com')")
driver.switch_to.window(driver.window_handles[-1])
# now your driver is pointed to the "tab" you just opened
Run the following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Get web driver going
cp = webdriver.ChromeOptions()
cp.add_argument("--browser.download.folderList=2")
cp.add_argument("--browser.helperApps.neverAsk.saveToDisk=image/jpg")
cp.add_argument("--browser.helperApps.neverAsk.saveToDisk=image/png")
cp.add_argument("--browser.download.dir=~/Downloads/")
driver = webdriver.Chrome(chrome_options=cp)
driver.get("http://www.google.com")
# Try to open a new tab
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL, 't')
This was an attempt to open a new tab, but the code does not work. This is also the case when trying to use Firefox. For Firefox, this does work if I don't change the profile (using equivalent code), but does not work with a custom profile.
I would also like to be able to send Ctrl+S too, but it seems no commands involving a special character work (though I can still send_keys normal text, not involving special keys like Ctrl).
What can I do to be able to send Ctrl+T and Ctrl+S (especially the latter)?
You can use action chain as given below.
ActionChains(driver).key_down(Keys.CONTROL).send_keys('s').key_up(Keys.CONTROL).perform()