I have been searching for this a lot, but could not find an answer for Python.
Is it possible to simulate right click, or open up the context menu via selenium/chromedriver?
I have seen options for Java, and some other languages, but never in Python.
What would I have to do to simulate a right click on a link, or a picture?
It's called context_click in selenium.webdriver.common.action_chains. Note that Selenium can't do anything about browser level context menu, so I assume your link will pop up HTML context menu.
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
actionChains = ActionChains(driver)
actionChains.context_click(your_link).perform()
To move through the context menu we have to use pyautogui along with selenium. The reason for using pyautogui is that we need to have control of the mouse for controlling the options on the context menu. To demonstrate this, I am going to use a python code to automatically open a google image of Avengers Endgame in new tab.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import pyautogui
URL = 'https://www.google.com/'
PATH = r'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
action = ActionChains(driver)
driver.get(URL)
search = driver.find_element_by_name('q')
search.send_keys('Avengers Endgame')
search.send_keys(Keys.RETURN)
image_tab = driver.find_element_by_xpath('//a[text()="Images"]')
image_tab.click()
required_image = driver.find_element_by_xpath('//a[#class="wXeWr islib nfEiy mM5pbd"]')
action.context_click(required_image).perform()
pyautogui.moveTo(120, 130, duration=1)
pyautogui.leftClick()
time.sleep(1)
pyautogui.moveTo(300,40)
pyautogui.leftClick()
Now in the above code, the part till pyautogui.moveTo(120, 130, duration=1) is selenium based. Your answer begins from pyautogui.moveTo(120, 130, duration=1) and what this does is simply moves the mouse button to the open image in new tab option of the context menu(Please note that the screen coordinates may vary based on your screen size). The next line clicks the option(using action.click().perform() won't work as expected).
The next two lines helps in navigating to the tab after it opens. Hope the code helps!
I encountered the same issue where I had to right-click and click on 'open link in new tab'.
I searched for a lot of answers on google but there was no specific solution I found for Python.
Earlier, I was doing using ActionChains where right-click menu is showing, but then that menu list can't be accessed in selenium as I found some threads saying this has OS-level access.
action = ActionChains(driver)
action.context_click(<obj_which_u_want_to_click>).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
Here, Keys.ARROW_DOWN is not working, and opening the link in the same tab, ideally, it should open in a new tab.
So, there are two ways through which I did this:
First, via send_keys:
link = driver.find_elements_by_xpath("//a[contains(#href, 'https:...')]")
link.send_keys(Keys.CONTROL + Keys.ENTER)
Second, through JavaScript:
driver.execute_script("window.open(arguments[0], '_blank');", link)
I think you can't access the right-click menu items in selenium as it is out of its scope.
You can perform context click using ActionChains, and use Arrows via send_keys to select an element from the context menu.
ActionChains(context.browser).move_to_element(element).context_click(element).perform()
ActionChains(context.browser).send_keys(Keys.ARROW_UP).perform()
ActionChains(context.browser).send_keys(Keys.ENTER).perform()
Related
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.
I started to learn scrape websites with Python and Selenium. I choose selenium because I need to navigate through the website and I also have to login.
I wrote an script that is able to open a firefox window and it opens the website www.flashscore.com. With this script I also be able to login and navigate to the different sports section (main menu) they have.
The code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# open website
driver = webdriver.Firefox()
driver.get("http://www.flashscore.com")
# login
driver.find_element_by_id('signIn').click()
username = driver.find_element_by_id("email")
password = driver.find_element_by_id("passwd")
username.send_keys("*****")
password.send_keys("*****")
driver.find_element_by_name("login").click()
# go to the tennis section
link = driver.find_element_by_link_text('Tennis')
link.click()
#go to the live games tab in the tennis section
# ?????????????????????????????'
Then it went more difficult. I also want to navigate to, for example, the sections "live games" and "finished" tabs in the sports sector. This part wouldn't work. I tried many things but I can't get into one of this tabs. When analyzing the website I see that they use some Iframes. I also find some code to switch to a Iframes window. But the problem is, I can't find the name of the Iframe where the tabs are that I want to click on. Maybe the Iframes are not the problem and do I look to the wrong way. (Maybe the problem is caused by some javascript?)
Can anybody please help me with this?
No, the iframes are not the problem in this case. The "Live games" element is not inside an iframe. Locate it by link text and click:
live_games_link = driver.find_element_by_link_text("LIVE Games")
live_games_link.click()
You may need to wait for this link to be clickable before actually trying to click it:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
live_games_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "LIVE Games")))
live_games_link.click()
I am trying to render a page which on load automatically shows the print dialog. I want to skip it using selenium. I searched on internet but couldn't find any suitable example since most of example handles javascript alert not windows alert.
I also install python robotframework because many people suggested it but couldn't find any example for that either.
My question is how to dismiss windows alert using selenium and robotframework in python?
NOTE: This question is in continuation of my previous question. python selenium not updating pop up window url
I could avoid the printing window by overriding the window.print function:
driver.execute_script("window.print = function(){};")
Using Firefox you could set some profile options that effectively silence the print dialog popup.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
profile = webdriver.FirefoxProfile()
profile.set_preference("print.always_print_silent", True)
profile.set_preference("print.show_print_progress", False)
driver = webdriver.Firefox(profile)
driver.get("http://www.google.com")
# Send print instruction
elem = driver.find_element_by_xpath("//body")
elem.send_keys(Keys.COMMAND, "p")
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.
I have a question about how to launch the print dialog from chrome browser. I know the shortcut key to open this is ctrl+p, but I don't know how to describe it in selenium. Does anyone know this? Thanks a lot!
I tried the following code, but it doesn't work on my Chrome browser.
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name('body'))
actions.key_down(Keys.CONTROL).send_keys('T').key_up(Keys.CONTROL).perform()
Basically you need to trigger the JavaScript function that triggers the print pop-up. That function is window.print().
So what you need is to trigger that function after arriving at the page you want to print.
So say you want to print the front page of stackoverflow.com
driver.get("https://stackoverflow.com")
# now you're at the page you want to print. Trigger print function
driver.execute_path("window.print()")
Now it should prompt you to the print pop-up. The reason you need to trigger the JavaScript function is because JavaScript is the browser's language.
Not quite what you are asking about, but this is what worked for me in Firefox.
Send CTRL+P (or COMMAND+P on mac) to the body element using ActionChains:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://google.com")
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name('body'))
actions.key_down(Keys.CONTROL).send_keys('p').key_up(Keys.CONTROL)
actions.perform()
driver.get("https://stackoverflow.com")
driver.execute_script("window.print()")