I' writing automated tests using selenium in RIDE. I need somehow to right click element on page and click option from context menu.
Is somewhere any library for Robot Framework which may be helpful to do that?
If not, Could you help me how to do that in other way, using existing keywords for example?
I found the solution. I wrote an extension to the Selenium2Library:
from robot.api.deco import keyword
from selenium import webdriver
from selenium.webdriver import ActionChains
from Selenium2Library import Selenium2Library
class ExtendedSeleniumLibrary(Selenium2Library):
#keyword("Right Click Element")
def Right_Click(self, xpath):
driver = self._current_browser()
actionChains = ActionChains(driver)
element=driver.find_element_by_xpath(str(xpath))
actionChains.context_click(element).perform()
Now, I'm not using Selenium2Library but my ExtendedSeleniumLibrary with new method in class and it works.
Open Context Menu keyword from SeleniumLibrary.
Robot tec:
WebElement SighnPad = (appium.findElement(By.id(Lib.getProperty(CONFIG_PATH, "Sighnparent"))). //parent
findElement(By.className(Lib.getProperty(CONFIG_PATH, "sighnchild")))); //child
SighnPad.click();
Robot rightclick = new Robot();
rightclick.delay(1500);
rightclick.mousePress(InputEvent.BUTTON1_DOWN_MASK);
rightclick.mouseMove(630, 420);
rightclick.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Related
I was using Selenium find_element_by_xpath to click the cookie popup "Accept" button. But it was failing to find the button. So, I tried to locate the button using querySelector in the dev console. Also, the querySelector failed to find the button but after clicking inspect on the button the querySelector able to find the button.
Also, searching the xpath in dev elements just showing 1 of 1.
Why this is happening ? How do I click the "Accept" button using selenium ?
Website link: https://www.transfermarkt.com/
The xpath: //*[#id="notice"]/div[3]/div[2]/button
After inspect on the button.
That element is inside an iframe, so to access it you will first have to switch to that iframe.
You didn't mention what language are you using so I will give my solution in Python. This can be done with other languages as well with some little syntax changes.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'iframe[title="SP Consent Message"]')))
wait.until(EC.visibility_of_element_located((By.XPATH, '//button[#title="ACCEPT ALL"]'))).click()
When finished working inside this iframe you will have to switch back to the default content with
driver.switch_to.default_content()
I am new to using the selenium module. I have started with some simple tutorials which go ok until I get a popup.
Because the popup does not have an x, I am not able to apply other advice I have found online.
How to close pop up window in Selenium
However I have tried to inspect the code of the popup window and I can see that maybe I have a couple of options, close by referencing the link text 'Accept Cookies', or close by the button id which is "onetrust-accept-btn-handler"
This is the code I have so far.
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.reed.co.uk/jobs/senior-insight-analyst/42347955")
driver.implicitly_wait(10)
link = driver.find_element_by_link_text("onetrust-accept-btn-handler")
link.click()
Trying
link = driver.find_element_by_link_text("onetrust-accept-btn-handler")
and
link = driver.find_element_by_link_text("Accept cookies")
Result in errors
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"onetrust-accept-btn-handler"}
or
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Accept cookies"}
I am wondering if there is any better command than link text if the button id is known, or am I missing another step in the process because it is a pop up? Any help appreciated. Thank you.
Please use the explicit wait so that your popup window can come up and your selenium script can detect the element and click on it.
Use the below code -
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
Mark it as answer if it resolves your problem.
Thank you Swaroop Humane and Dev for your answers. The answer above works, I also needed to add in three more lines of import code before the solution managed to click the Accept cookies button in the pop up. https://selenium-python.readthedocs.io/waits.html
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Final code below.
import selenium
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.reed.co.uk/jobs/senior-insight-analyst/42347955")
#driver.implicitly_wait(10)
#link = driver.find_element_by_link_text("onetrust-accept-btn-handler")
#link.click()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
Then this happened.
I probably need to figure out a way to run selenium while I am signed into this webservice, rather than let it run a new browser every time. Any ideas?
I am stuck and I would love your help. I am not a Python genius, so apologies for the language. I need to click on a button (Export) on this website https://www.fec.gov/data/filings/?data_type=processed&committee_id=C00097485. The button should drive to the bottom of the page where a link to an Excel file appears. Now, I have used this code:
text="//button[#type='button' and contains(.,'Export')]"
driver = webdriver.Firefox()
driver.get("https://www.fec.gov/data/filings/data_type=processed&committee_id=C00142711")
time.sleep(5)
button=driver.find_element_by_xpath(text)
button.click
The script runs fine, no error messages. The website appears, but the 'click' doesnt take place.
I also tried:1) the "wait driver until element is clickable", 2) the ActionChain to move the cursor, 3) to substitute click with sendKeys.
There is no Iframe. I tried also on Chrome. I am using a pc with Windows 10.
What am I doing wrong??? Considering that with other websites, the click function works perfectly fine!
Click is a method so it should be button.click(). You are missing parens.
Also, it would be better if you used WebDriverWait instead of .sleep(), e.g. button = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, text)));
As per the url the button with text as Export is a JavaScript enabled element, so you need to induce _WebDriverWait_for the element to be clickable and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://www.fec.gov/data/filings/?data_type=processed&committee_id=C00097485")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.js-export.button.button--cta.button--export"))).click()
Browser Snapshot:
I am writing UI tests for my QT app with selenium + qtwebdriver, and I have one moment in app, which I need to test: user can perform right mouse button click -> some menu showed up and user can click in this menu. I tried this code:
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import time
driver = webdriver.Remote(command_executor='http://127.0.0.1:9517',
desired_capabilities={"browserStartWindow": "*", "reuseUI": True})
driver.get("qtwidget://MainWindow")
action = webdriver.ActionChains(driver)
action.move_by_offset(7, 87).context_click().move_by_offset(10, 91).click().perform()
Context menu appears (so it means, that right button click was performed well, but left mouse click was not performed. How to fix this ? Or maybe I can use other solution ?
Try using 'ARROW_DOWN' to select the option after clicking context click.
action.move_by_offset(7,87).context_click().contextClick().sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
In our application, we have a link called forgot password? and with contextClick() I am able to do right click on the link and but could not able to open the same link either in New tab or window.
Code which i used is as below.
WebElement wbLink=d.findElement(By.xpath("My xpath here"));
Actions act=new Actions(d);
act.contextClick(wbLink).sendKeys("W").build().perform();
//act.contextClick(wbLink).sendKeys("T").build().perform();
And also tried below way as well.
Actions act=new Actions(d);
act.contextClick(wbLink).sendKeys(Keys.chord(Keys.CONTROL,"T")).build().perform();
Any suggestions would be appreciate on this.
Instead of using contextClick(), you can open link in new tab using sendKeys() as below :-
import org.openqa.selenium.Keys;
String keys = Keys.chord(Keys.CONTROL,Keys.RETURN);
WebElement wbLink=d.findElement(By.xpath("My xpath here"));
wbLink.sendKeys(keys);
Note :- Use Keys.COMMAND instead of Keys.CONTROL if you are using mac