Element is not Visible error even if my xpath is unique - xpath

Element is not Visible error even if my xpath is unique
//System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
//WebDriver driver= new FirefoxDriver();
//WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.goindigo.in/");
driver.findElement(By.linkText("One Way")).click();
driver.findElement(By.xpath("//button[#class='btn buttonGlbl btn-close button-trigger']")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='innertab-content one-way-tab-ctnt']//li[#class='city-dropdown destination-dropdown ps-destination-dropdown without_label']//div[#class='city-dropdown-list city-name-to']//ul//li[3]")).click();
}
}//

/System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
//WebDriver driver= new FirefoxDriver();
//WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.goindigo.in/");
WebElement onewayLink=driver.findElement(By.linkText("One Way"));
onewayLink.click();
WebElement btnElement=driver.findElement(By.xpath("//button[#class='btn buttonGlbl btn-close button-trigger']"));
WebDriverWait wait= new WebDriverWait(driver,20 );
wait.until(ExpectedConditions.visibilityOf(btnElement));
btnElement.click();
driver.findElement(By.xpath("//div[#class='innertab-content one-way-tab-ctnt']//li[#class='city-dropdown destination-dropdown ps-destination-dropdown without_label']//div[#class='city-dropdown-list city-name-to']//ul//li[3]")).click();
}
}//

Above answer, no one hit the root cause, it has nothing to do with need to wait some element.
The root cause is you need to click on the input box of From/To city to make the city list pop-up, then click on the city you wanted.
This is a selenium design principle: an element must be visible, before selenium to click it.
The selenium desinger hope it can simulator operations from user perspective, as we know user need to see the city list before he can choose one. So we also need to make selenium 'see' the city list at first.
But one exception, when you want to select an option from a Select, you can
directly click on the option your wanted, no need to make the options pop-up firstly.
you can find such question from below link, and I give out the detail answer for it.
Using firefox webdriver trying to load the option Name from dropdown but not working?
Code snippet to fix your issue:
driver.get("https://www.goindigo.in/");
// click 'One Way' tab
driver.findElement(By.css(".one-way-tab")).click();
// Click 'OK' on Information pop-up
driver.findElement(By.css(".buttonGlbl.btn-close")).click();
// Click 'From' city input box for 'One Way'
driver.findElement(By.css("#oneWay .origins-value.city-name-value")).click();
// Choose city 'Delhi'
driver.findElement(By.css("#oneWay .origin-city-name"))
.findElement(By.partialLinkText("Delhi"))
.click();
// Click 'To' city input box for 'One Way'
driver.findElement(By.css("#oneWay .destinations-value.city-name-value")).click();
// Choose city 'Indore'
driver.findElement(By.css("#oneWay .dest-city-name"))
.findElement(By.partialLinkText("Indore"))
.click();

Related

Unable to locate the requested element: By.xpath: //*[#id="SF-Refresh"]

I have been trying to get my Automation Tests up and Running but keep running into some Xpath Errors. I am having 5 errors all with missing/not being able to locate the Xpath that I have wrote. I need to have my test click on a "Refresh" Button located in a data table that is located on the Web page. Button instead of clicking on the button I get the error below. The ID of the button is "SF-Refresh" .
#When("I click refresh button")
#Given("I click refresh button")
#Then("I click refresh button")
public void clickRefreshButton() {
By linkSelect = new By.ByXPath("//*[#id=\"SF-Refresh\"]");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LightningButton.click(linkSelect);
}
Error:
Unable to locate the requested element: By.xpath: //*[#id="SF-Refresh"]
There is few possible error as below:
1) The Xpath is might wrong or same Xpath return multiple elements, the xpath must return one element only if you are using findElement
2) The element is might not load properly yet or not ready for action use below:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id="SF-Refresh"]")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id="SF-Refresh"]")));
LightningButton.click(linkSelect);

Unable to click on Sign In button through xpath & AndroidUIAutomator

I am unable to click on sign in button in mobile using Appium.
I have used xpath & androidUiAutomator but unable to succeed.
Following are the details:
For Xpath i used:
//android.widget.TextView[#text='Sign In']
For AndroidUiAutimator i used:
return driver.findElementByAndroidUIAutomator("text(\"Sign In\")");
public void MainLogin() throws InterruptedException {
System.out.println("Enter in Epcl_MainLogin");
TouchAction touch=new TouchAction(driver);
WebDriverWait wt=new WebDriverWait(driver,20);
LoginPom objLogin = new LoginPom(driver);
wt.until(ExpectedConditions.visibilityOf(objLogin.LoginName()));
objLogin.LoginName().sendKeys("Testa");
TimeUnit.SECONDS.sleep(2);
driver.hideKeyboard();
objLogin.LoginPassword().sendKeys("Testb");
TimeUnit.SECONDS.sleep(4);
driver.hideKeyboard();
objLogin.BtnLogin().click();
System.out.println("Button Clicked Successfully");
TimeUnit.SECONDS.sleep(8);
System.out.println("Login Successfully");
}
you can add content-description to your sign in button (in your android code). Then you will be able to click the button by using
driver.findElementByAccessibilityId("your content description").click()
You can also add id to your button and click it using:
driver.findElementById("you button id").click();
Using xpath in appium is not recommended

Selenium "Element is not clickable at point" error in Firefox

In regards to the Webdriver error
Element is not clickable at point (X, Y). Another element would recieve the click instead.
For ChromeDriver, this is addressed at Debugging "Element is not clickable at point" error, however the issue can occur in Firefox as well.
What are the best ways to resolve this when it occurs in FirefoxDriver?
This happens in the below cases-
When the element is loaded into the DOM, but the position is not
fixed on the UI. There can be some other div or images that are not
loaded completely.
The page is getting refreshed before it is clicking the element.
Workaround
Use Thread.sleep before actions on each web element in UI, but it is
not a good idea.
Use WebDriverWait ExpectedConditions.
I was facing the same issue, the page load time was more and a loading icon was overlapping on entire web page.
To fix it, I have implemented WebDriverWait ExpectedConditions, which waits for the loading icon to disappear before performing click action on an element
Call this function before performing an action (I am using data driven framework)
public void waitForLoader () throws Exception {
try {
String ObjectArray[]=ObjectReader.getObjectArray("LoadingIcon");
if(checkElementDisplayed(ObjectArray[3],ObjectArray[2]))
{
WebDriverWait wait = new WebDriverWait(remotewebdriver,10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(ObjectArray[3])));
}
} catch (NoSuchElementException e) {
System.out.println("The page is loaded successfully");
}
}
If your problem is that the element is scrolled off the screen (and as a result under something like a header bar), you can try scrolling it back into view like this:
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY();
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click(); }
if you need you could also add in a static offset (if for example you have a page header that is 200px high and always displayed):
public static final int HEADER_OFFSET = 200;
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET;
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
You can direct click using JavascriptExecutor (Not recommanded)
WebElement element= driver.findElement(By."Your Locator"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)
My same problem is solved by Javascript, Please try following code instead of selenium click
WebElement rateElement = driver.findElement(By.xpath(xpathContenRatingTab));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", rateElement);
Careful matching of the Selenium jar version with the Firefox version can fix the issue. Selenium should automatically scroll an element into view if it isn't on the page. Forcing an element into view with JavaScript is unnecessary.
We never see this issue in Firefox 31.5.0 with selenium-server-standalone-2.44.0.jar, however when upgrading to Firefox 38.7.0 with selenium-server-standalone-2.52.0.jar, it became an issue.
See https://github.com/seleniumhq/selenium/issues/1543
I had the same problem and I solved it using certain capability. While you are using FirefoxDriver, you can set "overlappingCheckDisabled" to true to solve your problem.
capabilities.setCapability("overlappingCheckDisabled", true);
ActionBuilder can resolve the error. Sometimes there is another element in front of the object that needs to be clicked, so an ActionBuilder click to the location of the element may work in cases where a traditional click fails
Actions actions = new Actions(driver);
actions.moveToElement(clickElement).click().perform();
or try the middle of the element
Actions actions = new Actions(driver);
Integer iBottom = clickElement.getSize().height;
Integer iRight = clickElement.getSize().width;
actions.moveToElement(clickElement, iRight/2, iBottom/2).click().perform();
This Error coud ocur when for example u make to many accesses to some service , for example if u are making as I a bot .... For example instagram will block u for some period if u ar taged as blocked and then that error coud ocour not allowing u to click some elements in the page.
Try make another acount and switch to a vpn becouse probably your ip is already marked as blocked
Try to maximize the browser when you are working with resolutions greater than 1024x768. It works for me in js.
driver.manage().window().maximize();

How to write an xpath for getting more colors in an web page

How to write an x path for getting more colors in an web page
(for example)This is the web page I am getting two color,for this I need to know how can identify through x path.
(http://www.flipkart.com/comfort-boxer-black-casual-shoes/p/itme84ykdubkghkm?pid=SHOE84YKXF7MXMMK&&sizeSelected=true&otracker=pp_shoe_size )from this page.
Below code work for me:-
driver.get("http://www.flipkart.com/comfort-boxer-black-casual-shoes/p/itme84ykdubkghkm?pid=SHOE84YKXF7MXMMK&&sizeSelected=true&otracker=pp_shoe_size");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List<WebElement> allOptions = driver.findElements(By.xpath("//div[#class='big selector-paletteImage']"));
System.out.println("Count of colour ="+allOptions.size());
for ( WebElement we: allOptions) {
System.out.println(we.getAttribute("data-selectorvalue"));
}

How to test AjaxEventBehavior("onClick") for Apache Wicket radio button?

I'm using apache wicket and I run into trouble regarding testing the AjaxEventBehavior for a Radio button. Actually I want to test the "onClick" event like in my case when I select/click a radio button from a RadioGroup a specif page is rendered.
Code snippet:
RadioGroup<Boolean> selectPageRadioGroup =
new RadioGroup<Boolean>("selectPageRadioGroup", new Model<Boolean>(Boolean.TRUE));
selectPageRadioGroup.setDefaultModel(new Model<Boolean>(Boolean.TRUE));
final Radio<Boolean> radioButton1 =
new Radio<Boolean>("radioButton1", new Model<Boolean>(Boolean.FALSE));
radioButton1.add(new AjaxEventBehavior("onclick") {
#Override
protected void onEvent(AjaxRequestTarget target) {
setResponsePage(MyWebPage.class);
}
});
selectPageRadioGroup.add(radioButton1);
Assuming you have already done
WicketTester tester = new WicketTester();
tester.startPage(PageContainingRadioButton.class);
or a similar startPanel (Wicket 1.4) or startComponent (Wicket 1.5), so that your test has rendered a page containing the button at a known path you should be able to make WicketTester simulate the ajax behavior by something like
tester.executeAjaxEvent("blabla:form:selectPageRadioGroup:radioButton1", "onclick");
(You'll need to adjust that path of course.)
and then check that it did the right thing with
tester.assertRenderedPage(MyWebPage.class);

Resources