Unable to click on a button through any object locators? - java

I am trying to execute below Selenium Web driver script, But I am getting
org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible
<span class="button-inner">Login</span>
#Test
public void BrowserInvocation() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\DOGETHER\\Desktop\\Website\\chromedriver.exe");
#SuppressWarnings("unused")
ChromeDriver driver=new ChromeDriver();
driver.get("http://phasorlab-web-dev.s3-website-us-east-1.amazonaws.com/"); /*Get URL */
driver.manage().timeouts().implicitlyWait(65, TimeUnit.SECONDS);
driver.findElementByXPath("//span[#class='button-inner']").click();
//driver.findElementByClassName("button-inner").click();
//driver.findElementByTagName("[text()='Login']").click();

There are two problems with your locators.
Your XPATH //span[#class='button-inner'] identifies two nodes on the DOM. The first node is hidden and hence the exception ElementNotVisibleException. Same is case with driver.findElementByClassName("button-inner")
findElementByTagName("[text()='Login']") is incorrect because findElementByTagName only needs the tag name and is mostly used when you want to get a list of elements of a particular tag. For example, driver.findElementByTagName("a") would give you a list of all the links on the page
Solution:
Using the XPATH //span[#class='button-inner' and text()='Login'] uniquely identifies the right element.
Or simply //span[text()='Login']
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Login']")

You could use explicit wait for handling the exception, e.g. if WebDriver instance is driver.
WebDriverWait wait=new WebDriverWait(driver,//mention the time);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(//mention the By locator));
Refer following link to see available expected conditions for explicit wait.

The text login is in uppercase "LOGIN".
so try this:-
//span[text()='LOGIN']
The right way is to use the ID of the element because there are higher chances of text to get change.

Related

Selenium webdriver (java) - Click on desired link of some search results

I am doing automation using selenium webdriver (java) on a search engine BookMyCrop (http://www.bookmycrop.com). Here, I searched for a crop but, I am not able to click on desired search result. Please help me with it.
Code :
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
------My 1st try-------------
//WebElement link = driver.findElement(By.xpath("\"//div[#id = 'Links']/a[3]\""));
//link.click();
------My 2nd try-------------
//List<WebElement> find = driver.findElements(By.xpath("/html/body/section[2]/div[2]/div/div/div/div[1]"));
//find.get(1).click();
}
} –
You can use the css selector based on class names: ".product-block.inner-product-block" and get the list of all the search results.
Then click on whatever index you want to click.
I am not using an IDE for this but it would look something like this:
driver.findElements(By.cssSelector(".product-block.inner-product-block")).get(0).click();
As said, you can try with css ".product-block.inner-product-block"
Then
get List of WebElements
do loop
inside loop, try get text of each element or innerText attribute
cross check if it is required one or not by simple if condition
If so, click on that web element and break loop
if this locator is not giving required info, try other locator. say $$("a h3") for veg names.
The below code worked for me. It is navigates to the correct link
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.bookmycrop.com");
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();

What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver?

I am using Java and Selenium Webdriver in order to test the functionalities of a single page web application.
For this reason, clearly, elements are injected and removed from the DOM dynamically.
I know I can wait for an element to be present in the DOM using similar code that is using WebDriverWait (very neat template I wrote slightly changing GitHub):
public void waitForElement() throws Exception {
/*
Inject the following snippet in any web page to test the method
<button class="i-am-your-class" onclick="alert('Wow, you pressed the button!');">Press me</button>
*/
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
WebDriver driver = getDriver();
WebDriverWait wait = new WebDriverWait(driver, 10); // 10 can be reduced as per Test Specifications
driver.get("http://www.google.com");
WebElement response = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='i-am-your-class']")));
response.click();
System.out.println("*****************************************************************************");
System.out.println(response);
System.out.println(response.getText());
driver.close();
}
What I would like to know is if this is also the more efficient way to obtain such result using an xpath.
I have been researching on Stackoverflow and several answers point in a similar direction but no answer is focused on efficiency / performances:
WebDriver - wait for element using Java
Selenium WebDriver: wait for element to be present when locating with WebDriver.findElement is impossible
Thanks for your time and help.
There are a couple of facts which you need to consider as follows :
WebDriverWait() for 100 shouldn't be a real-time waiter. Consider reducing it as per the Test Specifications. As an example, set it as 10 seconds :
WebDriverWait wait = new WebDriverWait(driver, 10);
Moving forward as you are invoking click() method so instead of ExpectedConditions as visibilityOfElementLocated() method use elementToBeClickable() method as follows :
WebElement response = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class='i-am-your-class']")));
Optimize your xpath including the tagName as in By.xpath("//tagName[#class='i-am-your-class']"). As an example :
By.xpath("//a[#class='i-am-your-class']")
Optimize your code to invoke click() as soon as the element is returned through WebDriverWait as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='i-am-your-class']"))).click();
I do not know for what reason you have said XPATH is preferred. I would like to add something about locators.
The XPATH you have written in your code can be easily replaced by a css selector. Here is the example.
div[class='i-am-your-class']
Now the main question is why do you want to switch to different locators ?
more efficiency.
more fast as compared to xpath.
more reliable.
more performance.
You should always use locators in this order as suggested by Selenium contributors
ID
name
className
linkText
partialLinkText
tagName
cssSelector
XPath.
Note : Most of the time a cssSelector can replace Xpath, However Xpath has its own advantages which cssSelector do not provide.
For more reference you can go through this SO Link : Xpath vs Css selector

Selenium getting text input of Twingly (Java Code)

Please check out the element of this website.
It has a form, and along with 2 text input and 1 submit button.
I dont know which one from those 2 inputs that is actually used when the user type-in some urls over there.
But when I tried this (using firefoxDriver) to get the element:
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's unable to locate the element.
Then I change my code to this :
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's still unable to locate the element.
Any clues?
You haven't mentioned the exception which you are facing. As your input tag present under iframe so you need to first switch into frame and than have to perform actions -
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
//or you can use frame index as well
driver.switchTo().frame(0);
your element is available with the id ping-box . Try the following complete code -
System.setProperty("webdriver.gecko.driver","D:/Application/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.twingly.com/ping");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
driver.findElement(By.id("ping-box")).sendKeys("http://www.google.com");
driver.findElement(By.id("ping-button")).click();
Same is working for me.

selenium.ElementNotVisibleException: Element is not currently visible JAVA

I am working on selenium, while running Java code I tried to CLICK a menu from the web page but encounter error of selenium.ElementNotVisibleException: Element is not currently visible Kindly advise on this matters . Thanks you
HTML code for text field :
<li onclick="goin('pages/AbcProxy/proxyGroupList.do')>
TESTABC
JAVA code:
WebdriverWait wait = new WebDriverWait(driver,50);
wait until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
presenceOfElementLocated checks if the element exists in the DOM. To check if the element is visible use visibilityOfElementLocated
WebdriverWait wait = new WebDriverWait(driver,50);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
element.click();
It might be that your xpath is to a different a element that is invisible (css display none or such).
It would be better to use some id first (maybe for the parent ul?) and then a relative xpath from there, full xpaths from root are not recommended as it makes the test very brittle

Selenium Web Driver: not able to find the element on the 2nd page.

I am using Java and Firefox and Firebug
I am not able to locate the element on the second page. For example if I login to gmail then I am not able to locate and click on the sent items or any other button on the next page.
I tried with the xpath (both absolute and relative) but every time I am getting an error that element not found.
with the code I am successfully able to login but as soon as the page loads I get an error "Element not Found".
Please suggest any solution
Unless you are telling WebDriver to wait until the element on the 2nd page is loaded, WebDriver will simply try to click the element as soon as its able to run. This is bad because your element might not yet be loaded while WebDriver is already trying to click it... TIMEOUT mayhem ensues...
Try the following... use the WebDriverWait class to make WebDriver wait for the element on the page to be loaded before attempting to click it...:
WebDriverWait wait = new WebDriverWait(driver, 100);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your xpath")));
element.click();
The '100' in WebDriverWait(driver, 100) is the maximum amount of seconds you want WebDriver to repeatedly attempt to locate the element before it times out...
I agree with the answer by CODEBLACK. Also you can go for Imlicit wait,which would make selenium wait implicitly for a given period of time.
Try following:-
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
You can specify time as per your convenience.
Best O Luck. . .!

Categories

Resources