I want to mimic Enter being pressed in Webtest. I am using Selenium 2.3.1. I want to do it using WebDriver. I know that we can do this using Selenium RC, but I do not want to do it that way. Has anybody done this before? I am open to upgrade to Selenium 2.20.0 (latest).
You can send an Enter key to an element. However, you can't press Enter to, say, confirm a download dialog.
WebElement elem = driver.findElement(By.id("damnit")); // obtain an element
elem.sendKeys(org.openqa.selenium.Keys.ENTER); // this sends an Enter key to the element
elem.sendKeys("hey" + org.openqa.selenium.Keys.ENTER); // this writes and then confirms by Enter
Related
Im trying to get into java/selenium but i have some problems i cant solve.
I cant interact with the "WEITER" Button from the website Snipes.com (https://www.snipes.com/checkout?stage=payment#payment). It works with every other button but the box of this one looks a little bigger than the button itself. Perhaps this is the problem but i dont know how i can only click the button.
The code im using for this part is following:
TimeUnit.MILLISECONDS.sleep(500); //select Paypal as Payment
selectPayment = driver.findElementByXPath("/html/body/div[3]/div[2]/div/div/div[2]/div[1]/div[3]/div[2]/div/div/form/div/ul/li[1]/div[1]/label");
js.executeScript("arguments[0].scrollIntoView(true);", selectPayment);
js.executeScript("arguments[0].click();", selectPayment);
TimeUnit.MILLISECONDS.sleep(500);
nextButtonCheckout = driver.findElementByXPath("/html/body/div[3]/div[2]/div/div/div[2]/div[1]/div[3]/div[2]/button");
nextButtonCheckout.click(); //click the "WEITER" Button
TimeUnit.MILLISECONDS.sleep(200);
Is there any way to click only the "main" button element instead of the whole thing so i can interact with it?
Error: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/div/div/div/div/div1/button"}
whole error: https://pastebin.com/jki8T8Y4
I want to click the little "x" on the top right of this pop up and sometimes it works but sometimes it doesnt. How can I test if it found the element and if not try again? I use proxies for this and the speeds are different so i cant just wait 5s and then check if it works. Sometimes the element can be found in 2s sometimes in 15s. I need to retry it a few times and my idea is to check it the variable 'selectRegion' if something is stored in it. I tried it with if(selectRegion =! "the output from the element") but i cant compare a WebElement with a String.
the code i use:
//Snipes Region + accept cookies
driver.get("https://www.snipes.com/login");
TimeUnit.SECONDS.sleep(7);
selectRegion = driver.findElementByXPath("/html/body/div[5]/div/div/div/div/div[1]/button");
selectRegion.click(); //click on the "x"
System.out.println(selectRegion); //get the variable output the use it later for the retries
acceptCookies = driver.findElementByXPath("/html/body/div[2]/div[2]/div/div[2]/div[2]/button");
acceptCookies.click();
This is a common problem but does anyone know how i can use user:passw authenticated proxies with selenium? Everytime i start it there is this authentication pop up but i cant interact with it.
I m trying write a code to access Aliexpress and search for an item, then extract the details, such as, Product name, price, etc.; on page by page to an Excel document. I seek through previous questions posted here to build it. Thanks to that.
Somehow I was able to search the item for first 5 or 6 test runs but then suddenly, Aliexpress asked me to either login or register.
1.) First question, Why any browser won't access the website without registering? Did they recognized my user-agent?
2.) Secondly, Then I was wrote a code to auto log in. Site contains lots of Javascripts, an it is an responsive site. Some html elements appear as we click them. When in the auto log in, my code won't detect the either E-mail or password elements of the page. Is there something preventing it from been detected? How can I solve this?
I here put my sample code:
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
//To input the user's search
Scanner nw1 = new Scanner(System.in);
System.out.println("What do you want to search?");
String a = nw1.nextLine();
//Open the driver
System.setProperty("webdriver.chrome.driver",
"E:\\JetBrains\\webdriver\\chrome\\chromedriver.exe");
WebDriver AE = new ChromeDriver();
//Open the web page and Login in.
AE.get("https://www.aliexpress.com/");
Thread.sleep(2000);
//xpath of account button
AE.findElement(By.xpath("//*[#id="nav-user-account"]/div/div/p[3]/a[2]")).click();
//xpath of Sign in button
AE.findElement(By.xpath("/html/body/div[9]/a")).click();
AE.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//xpath of Email box
AE.findElement(By.xpath("//*[#id=\"fm-login-id\"]")).sendKeys("my-email");
//xpath of password section to type
AE.findElement(By.xpath("//*[#id=\'fm-login-password\']")).sendKeys("my-password");
AE.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// xpath of submit button
AE.findElement(By.xpath("//*[#id="login-form"]/div[5]/button")).click()
Sry kind of my first time here.
Any helpful comments are welcome. Thanks.
It is because following x path in not clicking on account button instead it is clicking on sign in button
AE.findElement(By.xpath("//*[#id="nav-user-account"]/div/div/p[3]/a[2]")).click();
find element through id. Id is available to click on account
"nav-user-account"
while clicking verify that it is unfold through following class
"ng-item nav-pinfo-item nav-user-account user-account-unfold"
if unfold contain than box is open otherwise it is close. If close than click on it.
Try this one first.
Try applying delete cookies ,some sites access data from cache /cookies ,clearing it would solve.Also quit driver at the end of your script and open a new instance i.e driver.manage().deleteAllCookies(); and driver.quit();
xpath in your code needs to be corrected as AE.findElement(By.xpath("//*[#id='nav-user-account']/div/div/p[3]/a[2]")).click();
Use single quotes inside of your xpaths.
I am using mixture of autoit + selenium where i am trying to automate Oracle Flexcube application which is like java Applet.
I have used JVM-Inspector for identifying the swing/AWT elements of the UI.
WebElement textF = aDriver.findElement(By.xpath("//*[#instance = '214']"));
textF.clear();
textF.click();
aDriver.getKeyboard().sendKeys(new String[] { "01/02/2018"});
Here cursor is moving to the VTextField (as shown in the jvm-inspector) , but clear() is not working and also sendKeys() is also not working.
I have tried everything but keyboard gets lock. If i put Thread.sleep(5000); and try to enter keys from the keyboard while execution is going on , keyboard gets lock. Here aDriver is autoitdriver.
i'm currently testing the GUI of my application, and i wanted to know if it's possible to set the focus to a WebElement ?
I'm using Selenium 2.0 and the webdriver.
So i'm looking for something like that : driver.findElement(xxxx).setfocus();
Thank you.
EDIT :
I've alreardy tested that kind of tricky things
// getting the element WebElement
eSupplierSuggest = driver.findElement(By.xpath("..."));
//get le location and click
Point location = eSupplierSuggest.getLocation();
new Actions(driver).moveToElement(eSupplierSuggest, location.x, location.y).click();
//or
//directly perform
new Actions(driver).moveToElement(eSupplierSuggest).click().perform();
i red somewhere that the click focus the element, but in my case, nothing works. lol
PS : this is a sub-question of that original post Click on a suggestbox, webdriver
I normally send an empty key to the element so it gets focused. So for example:
element.send_keys ""
In order to set focus on the element you can use executeScript method as described below :
JavascriptExecutor js;
js.executeScript ("document.getElementById('x').focus()");
Once the focus is set you can easily use send_keys provided by webdriver api.
Try using cssSelector for the autosuggestion click as shown below and let me know if you are still facing the issue.
// supplier ops, i find and type data into the input
WebElement eSupplier = driver.findElement(By.id("supplier:supplierOps_input"));
eSupplier.sendKeys("OPS1");
sleep(5); // wait the suggestbox
// i find the suggestbox
WebElement eSupplierSuggest = driver.findElement(By.cssSelector("css path of that specific value in the auto suggestion box"));
eSupplierSuggest.click();
sleep(5); // wait the refresh for the next field supplierAddress
There is no function in the WebDriver API to set focus on an element.
If you want to do it you would have to write some JavaScript to set focus and then use a JavaScriptExecutor to run the JavaScript.
Make sure, that you are not changing the frame....
Other wise .click() should do the trick
if I execute the following code in FireFoxDriver:
WebElement element = driver.findElements(By.id("some_id")); // element being a textbox
element.sendKeys("apple");
element.sendKeys(Keys.RETURN);
The sendKeys(Keys.RETURN) is not performing its desired function.
Actually what I am trying to do is Input a text in a dynamic text search box (like one in facebook search) and press enter. The input is working fine but not the enter key.
sendKeys("apple") works, even sendKeys(Keys.BACK_SPACE) works, but not Keys.RETURN.
Does anyone have ideas? Thanks guys!
Not exactly sure why this happens, but there are a couple alternate ways of doing this that may help:
If elements are in a form, and there is no javascript that runs on submit or something you can use .submit() on any form input element, such as inputs and textareas:
WebElement element = driver.findElements(By.id("some_id"));
element.sendKeys("apple");
element.submit()
You can send the newline character with your input:
WebElement element = driver.findElements(By.id("some_id"));
element.sendKeys("apple\n");
Provide send_keys a list:
WebElement element = driver.findElements(By.id("some_id"));
element.sendKeys("apple", Keys.ENTER);
Got the solution to the above problem. U just need to add, a delay.
This happens because the Java Class runs too fast, so if u have sent a call, and pressed enter/ tab, before the element arrives, the enter is pressed, that is why this doesn't work. Just add Thread.delay(1000); before your Keys.RETURN command. That will do.
Worked for me.
I tried sending \n and fiddled with various commands until I found someone explaining that "keyPress (target) 13" will send the return key.
So first I use type to enter the string I want ...
*
*<tr>
<td>type</td>
<td>id=status</td>
<td>This is my test string</td>
</tr>*
*
... and then send the Enter key to the same text input box
*
*<tr>
<td>keyPress</td>
<td>id=status</td>
<td>13</td>
</tr>*
*