I need to run Selenium in Linux machine and using HtmlUnitDriver to achieve this.
Selenium script contains most of Xpath to find the elements. While running, NoSuchElementException is getting displayed at the places where xpath is used.
org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[contains(text(),'Sample Text')]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
The same code is working fine with other drivers like Chrome, Firefox or IE.
My code looks as below,
HtmlUnitDriverdriver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("http://example.com");
driver.findElement(By.id("username")).sendKeys("xxx");
driver.findElement(By.id("password")).sendKeys("yyy");
driver.findElement(By.id("loginButton")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[contains(text(),'Sample Text')]")).click();
I found many similar question and the solution is to provide sleep time or to wait for the element till it gets loaded. I provided everything and still facing this issue when xpath is used.
Also finding the element by ID or name is not possible in my use case as it varies everytime.
I can use only the xpath to find the text inside the span tag.
I need a solution for handling this. Thanks in advance!
Edit :
I tried the same for gmail login, Still am getting the same issue,
driver.get("http://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("xxx#gmail.com");
driver.findElement(By.id("next")).click();
Thread.sleep(2000);
driver.findElement(By.id("Passwd")).sendKeys("yyy");
driver.findElement(By.id("signIn")).click();
Thread.sleep(8000);
driver.findElement(By.xpath("//*[contains(text(),'COMPOSE')]")).click();
Consider also getting rid of text(), which might cause that kind of problem. Use this instead:
contains(., 'Sample Text')
Not sure this is the cause if your problem here, this is more of a blind shot, given the few info you gave us.
I would first of all suggest providing a more effective explicit wait, so that it will suspend until the condition is fulfilled, and also will give you the TimeoutException if you didn't provide long enough timeout.
//This will store your locator for later use
By elementLocator = By.xpath("//*[contains(text(),'Sample Text')]");
//This creates a reusable wait object
WebDriverWait wait = new WebDriverWait(browser.driver, 15);
//Waiting is stopped right after the condition is satisfied or timeout (set in the previous line, in secs) was reached
wait.until(ExpectedConditions.elementToBeClickable(elementLocator));
If you are getting the same error you can try this
try {
wait.until(ExpectedConditions.elementToBeClickable(elementLocator));
} catch (org.openqa.selenium.TimeoutException e) {
System.out.println(driver.getPageSource());
}
To see whether the element is present on the page you are looking for it.
Related
In selenium testing framework I am using thread.sleep(40000). I have a requirement not to use thread.sleep()or its alternative fluent wait or any kind of wait, and keep your script engaged anyhow so that script will pick particular element after that some interval until that element actually appears on that page. Hence error wont be thrown while accessing the element. Do you have any suggestion how can I keep my script engaged for few miliseconds without using any wait ?
I use this code. You can add try catch with timeOutException.
import org.openqa.selenium.By.*;
By element = new ById("id");
long timeout;
WebElement webElement =(WebElement)(new WebDriverWait(getDriver(), timeout)).until(ExpectedConditions.visibilityOfElementLocated(element));
I am relatively new to Selenium Grid and I'm having issues with test stability. For some reason, when I'm running my tests via the grid and RemoteWebDriver, my tests intermittently get stuck on the IEDriverServer opening page that says,
"This is the initial start page for the WebDriver server."
This causes the remainder of the tests that get put against that machine to fail, which is extremely frustrating because 99 times out of 100 those same tests will pass on a rerun.
I am wanting to do something like the following but can't seem to implement it. Here is the pseudo code:
if(initialWebDriverPageTextIsOnPage > 30seconds)
{
driver.Close();
driver.Dispose();
}
I feel like this could fit in my basetest as a listener that consistently polls the page but I can't seem to figure out how to implement this.
Any advice, or suggestions as to what the root cause is would be greatly appreciated
As you want to wait for your intended url to wait for a definite period, you can induce ExplicitWait i.e WebDriverWait with proper ExpectedConditions clause as follows :
WebDriverWait wait_4_link = new WebDriverWait(driver, new TimeSpan(0,0,10));
if(wait_4_link.Until(ExpectedConditions.UrlToBe("your_intended_url")))
{
//Your Test Code
}
else
{
driver.quit();
}
As an alternative instead of the ExpectedConditions clause as UrlToBe you can also use either of the following clauses :
wait_4_link.until(ExpectedConditions.UrlContains("your_url_fraction"));
wait_4_link.until(ExpectedConditions.UrlMatches("regex_pattern_of_url"));
I am writing an automated test and want to report bugs, if occur, directly in the repo at GitHub. The step which fails in my program is the Submit new issue button from GitHub Issue Tracker.
Here is the code:
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
And the exception:
org.openqa.selenium.WebDriverException: Element is not clickable at
point (883, 547.7999877929688). Other element would receive the click:
div class="modal-backdrop"></div
The following command also does not work:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", sendIssue);
How can I make it clickable? Is there any other way by which I can resolve this issue?
This is happening because when selenium is trying to click ,the desired element is not clickable.
You have to make sure that the Xpath provided by you is absolutely right.If you are sure about the Xpath then try the following
replace
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
with
WebElement sendIssue =(WebElement)new WebDriverWait(DRIVER,10).until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button")));
sendIssue.click();
If that doesn't work ,You will get an Timeout exception, In that case try incaresing the timeout amount from 10 to 20.
If it still doesn't work please post a screenshot of the HTML.
You need to write something in the issue title and description to make the issue clickable are you sure you are not making that mistake of clicking the button without writing anything in those places I am adding screenshot for your convenience.
Selenium Webdriver introduced in a previous version (v2.48) a new behavior that prevent clicks on elements that may be overlapped for something else (a fixed header or footer - for example) or may not be at your viewport (visible area of the webpage within the browser window).
You can see the debate here.
To solve this you will need to scroll (up or down) to the element you're trying to click.
One approach would be something like this post:
Page scroll up or down in Selenium WebDriver (Selenium 2) using java
Another, and maybe more reasonable, way to create a issue on Github, would be using their API. Maybe it would be good to check out!
Github API - Issues
Gook luck.
This worked for me. Instead of HTML browser this would be useful if we perform intended Web Browser
// Init chromedriver
String chromeDriverPath = "/Path/To/Chromedriver" ;
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);
I am using selenium 2.46 (firefox driver) to develop an application. There are a lot of element.click() in my code. Sometimes that elements are not visible or not clickable make the application throws selenium exception.
To resolve that issue, i use WebdriverWait(driver, 10).until(...) for each single element which needs to be clicked.
My question is there is any other better way Or design pattern that can help me to solve the problem best.
Or at least i dont have to use WebdriverWait for each single element needs to be click().
You cannot avoid WebDriverWait. If you send a webdriver click command, webdriver will blindly assume that "element is clickable". You need to instruct webdriver to wait because your element is special and needs some synchronization before it can click on it. I don't think you need to do this for every other element. You can incorporate ExpectedConditions so that you can keep your code snippets manageable and small. So something like,
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("foo"))).click();
The other option you can try other than clicking is hit enter on respective element, for that you can refer ID of that element.
driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);
use implicit wait instead of explicit wait and give the expected condition till the element doesn't visible on screen.
for more info you can check
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#invisibilityOfElementLocated-org.openqa.selenium.By-
Hope this will help you
After implementing filepicker.io, some of our Selenium regression tests have started failing. The failures (intermittent, but more often than not in some circumstances) are that clicks are ignored on WebElements found via XPath queries. e.g.
driver.findElement(By.xpath("//a[text()='Demo data']")).click();
Adding a Sleep(2000) between findElement() and click() generally resolves the problem. (I say generally because Sleep(1000) was mostly enough, until it wasn't, so I made it Sleep(2000)...)
Checking element.isDisplayed() has not helped. The problem disappears if we stop including the filepicker.io JavaScript file.
Is it something to do with filepicker.io introducing an IFRAME? We have also noticed that JQuery's document.ready() seems to be now invoked twice.
As usual with this kind of problems, you are trying to find an element that is not yet available on the page due to AJAX request still downloading/processing it. You need to wait for the element to appear on the page.
There are three ways to do this:
Using sleep(). This is the discouraged way. You should not use hardcoded sleeps, because you'll either wait too long (making the tests unnecessarily slow) or too short (failing the test).
Use Implicit wait. That will always wait for an element if it's not found.
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Use explicit wait. That enables you to wait explicitly for one element to (dis)appear / become available / whatever.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Demo data")));
We now run this code first after opening any page that includes filepicker.js:
while (FindElementsMaybeNone(By.cssSelector("#filepicker_comm_iframe")).size() == 0)
Sleep(50);
while (driver.switchTo().frame("filepicker_comm_iframe") == null)
Sleep(50);
driver.switchTo().defaultContent();
We guess that filepicker's dynamic IFRAME insertion is discombobulating Firefox or Selenium. I'm not marking this as the answer because I don't really know why it works.