Selenium - Troubles when trying to use javascript-xpath instead of ajaxslt - selenium

I'm currently trying to improve my Selenium tests on IE. I would like to use javascript-xpath instead of ajaxslt.
My setUp function is :
public void setUp() throws Exception {
super.setUp(host, browser);
selenium.setSpeed(Constants.DELAY_BETWEEN_ACTIONS);
selenium.windowMaximize();
selenium.allowNativeXpath("false");
selenium.useXpathLibrary("javascript-xpath");
}
When I try to find an element by Xpath, for instance :
selenium.click("xpath=//a[#id='linkLogin']");
I get the error (only when i try to use javascript-xpath):
com.thoughtworks.selenium.SeleniumException: ERROR: Invalid xpath [2]: //a[#id='linkLogin']
What do i miss to correctly use javascript-xpath ?
Thanks for your help.

Have you considered changing your locator strategy away from xpath? I've been using a form of CSS locators that are on average 5 times faster than xpath.

Related

Problem locating element with xpath in Selenium

I am having some troubles to locate an element with Selenium.
The page html where I'm looking for it is:
enter image description here
Specifically, I want to get the input with id='filtroDNI'. For this I'm using
WaitFluent.xpathClickable("//input[#id='filtroDNI']")
But I'm getting Method threw 'org.openqa.selenium.TimeoutException' exception. because Expected condition failed: waiting for visibility of element located by By.xpath: //input[#id='filtroDNI'] (tried for 11 second(s) with 3000 milliseconds interval)
However, If I try to get some elements above this one, I can reach the div with id='content-wrap' by using following code:
WaitFluent.xpathClickable("//div[#id='content-wrap']")
Do you have any idea of what can be happening? Also I tried with alternative xpaths but the results are the same.
Also, the page is throwing an error but after consulting it with the dev team, they said it has nothing to be with my question, the error is this one:
Uncaught TypeError: Cannot read properties of undefined (reading 'hasChildNodes')
at sorter.sort (script.js:53:12)
at sorter.reset (script.js:43:9)
at sorter.init (script.js:37:8)
at new sorter (script.js:8:77)
at histo.php:277:54
Thanks in advance
Edit:
xpathClickable method code:
/**
* <STRONG>Description:</STRONG>Waits for an element with a given xpath to be clickable.
*
* #param xpath String;
* #return WebElement
*/
public static WebElement xpathClickable(final String xpath) {
return new FluentWait<WebDriver>(myEnvironment)
.withTimeout(IMPLICIT_TIMEOUT)
.pollingEvery(RETRY_TIME)
.ignoring(StaleElementReferenceException.class,
NoSuchElementException.class)
.until(ExpectedConditions
.elementToBeClickable(By.xpath(xpath)));
}
Edit 2:
DEV team already solved the JS issue shown in console, but the xpath cannot be retrieved yet.

While opening Gmail, following error occurs- Expected [object Undefined] undefined to be a string

Expected [object Undefined] undefined to be a string,
The code I am using is following:
System.setProperty("webdriver.gecko.driver","E:\\Software\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver wd= new FirefoxDriver();
wd.get("https://www.google.co.in/");
//wd.findElement(By.xpath(".//*
[#id='gbw']/div/div/div[1]/div[1]/a")).click();
wd.findElement(By.linkText("Gmail")).click();
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
e1.sendKeys("abc#gmail.com");
wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Ne
xt']")).click();
error log
error log
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)
at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.usingFirefoxBinary(GeckoDriverService.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:204)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:104)
at register_prctc.gmail.main(gmail.java:15)
Here is the solution to your Question:
To work with Selenium 3.4.0, geckodriver v0.16.1 & latest Mozilla Firefox 53.x you need to set the absolute path of the geckodriver in your code as:
System.setProperty("webdriver.gecko.driver","C:\\your_dir\\geckodriver.exe");
As per best practices you should not use Thread.sleep(6000), instead use ImplicitlyWait or ExplicitWait.
The xpath .//[#id='gbw']/div/div/div[1]/div[1]/a you used doesn't identifies any unique element. To find the element Gmail link you can use the linkText locator as:
wd.findElement(By.linkText("Gmail")).click();
For sending text into Email or Phone field provide a unique xpath as:
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
The xpath to click on Next button looks vulnerable to me, you may like to change it to : wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Next']")).click();
Here is the working set of your own code with some simple tweaks:
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
WebDriver wd= new FirefoxDriver();
wd.get("https://www.google.co.in/");
wd.findElement(By.linkText("Gmail")).click();
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
e1.sendKeys("id#gmail.com");
wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Next']")).click();
Let me know if this Answers your Question.
Remove Selenium-java-2.53.1.jar file and update all jars

JSONOBject hash not found

I am working on Test Automation Script using JAVA and Selenium WebDriver ,
My test is running on cloud environment (crossbrowsertesting.com).
There is an feature to take snapshots of browser window ,
When I was using RemoteWebDriver this line of code work fine , but need to replace it with WebDriver because reason not bale to get windowHandles.
But I am getting following error now , stating
"The method getSessionId() is undefined for the type WebDriver"
snapshotHash=myTest.takeSnapshot(driver.getSessionId().toString());
//takeSnapshot method :
public String takeSnapshot(String seleniumTestId) throws UnirestException {
System.out.println("Screen Shots Taken.");
/*
* Takes a snapshot of the screen for the specified test.
* The output of this function can be used as a parameter for setDescription()
*/
HttpResponse<JsonNode> response = Unirest.post("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}/snapshots")
.basicAuth(username, api_key)
.routeParam("seleniumTestId", seleniumTestId)
.asJson();
// grab out the snapshot "hash" from the response
snapshotHash = (String) response.getBody().getObject().get("hash");
return snapshotHash;
}
I dont quite understand why you need to use "WebDriver" in place of "RemoteWebDriver" ? "RemoteWebDriver" is the mother of all web driver implementations and it should be good enough to work with any remote grid environment. I dont understand why you need to switch to using "WebDriver" reference which is one of the interfaces that "RemoteWebDriver" implements. getSessionId() is NOT part of any interface specifications but its a direct implementation that RemoteWebDriver provides.
getWindowHandles() is part of WebDriver interface specification and you should still be able to use it.

Not able to write in inputbox of Google's homepage by WebDriver

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.xpath("//*[#id='gs_htif0']"))
.sendKeys("selenium");
I want to send to use xpath as a locater.
Error:
Exception in thread "main"
org.openqa.selenium.InvalidElementStateException: Element is disabled
and so may not be used for actions Command duration or timeout: 75
milliseconds
After that I want to get the google suggestions printed.
You can try using name instead of XPATH. Name selector is faster than Xpath.
driver.findElement(By.name("q"));
You are trying to access the google search bar by ID, try this id: //*[#id='lst-ib']
driver.findElement(By.xpath("//*[#id='gs_htif0']"))
.sendKeys("selenium");
You can also try these:
python: driver.find_element_by_xpath("//*[#name='q']").send_keys('selenium')
java: driver.findElement(By.xpath("//*[#name='q']")).sendKeys("selenium");
To get the suggestions you can try this:
List<WebElement> rows = driver.findElements(By.xpath("//*[#role='option']"));
for (int i=0; i<rows.size(); i++){
System.out.println(rows.get(i).getText());
}
Output: selenium selenium ide selenium tutorial selenium webdriver
First click in the input box and then use . sendKeys("whatever you want to type ")

Selenium webdriver: What is the substitute for browserbot?

I'm trying to convert some Selenium HTML tests to use the WebDriver 2.0 framework. According to the web site (http://seleniumhq.org/docs/03_webdriver.html), the WebDriver framework no longer supports the "browserbot" Javascript variable. So my question is, how do I convert a command like
<tr>
<td>verifyEval</td>
<td>this.browserbot.getUserWindow().s.pageName</td>
<td>Config_6_Summary_Confirm_EX</td>
</tr>
using WebDriver? When I run the command
String target = selenium.getEval("this.browserbot.getUserWindow().s.pageName")
commnand, I get an exception stating, "this.browserbot is undefined". Thanks, - Dave
I make a suggestion to following.
String target = selenium.getEval("window.s.pageName")
You can access to 'browserbot' from WebDriver's getEval by "selenium.browserbot".(not "this")
selenium.getEval("typeof(this.browserbot)"); // undefined
selenium.getEval("typeof(selenium.browserbot)"); // object
but, can not use some browserbot function.
(I don't know the deference of 'enabled function' and 'disabled function'. sorry)
"getUserWindow()" is disabled.
You can use a "window" instead of it.