Selenium Fluent WebDriver - Click does not work as expected - angularjs

I have a pagination list, that is essentially constructed like so (I am using AngularJS):
<span id="latestResultsIndicator">
<ul>
<li ng-click="Item.action()"><span>1</span></li>
<li ng-click="Item.action()"><span>2</span></li>
</ul>
</span>
This works great in HTML, but when I try to write an integration test using Selenium WebDriver and the Fluent API I run into problems.
Specifically I want to click on the second li, to do this I am using the following code
I.Assert.Exists("#latestResultsIndicator");
var secondPageElement = I.Find("#latestResultsIndicator ul li:nth-child(2)");
I.Click(secondPageElement);
This doesn't actually work! If I use jQuery in the Chrome console to do $("#latestResultsIndicator ul li:nth-child(2)").trigger("click") then the second page is selected, so I know the selector is correct.
To test further I added a double click like so:
I.DoubleClick(secondPageElement);
What I noticed here is that the very first li gets selected. Its as if its trying to click or select the wrong one! (See the image).
What is happening?

I know Webdriver has some issues with css pseudo-selectors such as nth-child. You might be able to make it work with Xpath, which is just as fast and just as flexible.
var webDriver = (IWebDriver)I.Provider;
webDriver.FindElement(By.XPath("//*[#id='latestResultsIndicator']//ul//li[2]")).Click();
Not sure if it will work or not, as I have not used FluentAutomation.

The CSS selector as it is written in your question is not correct
"#latestResultsIndicator ul li:nth-child(2)"
The UL has the id you're looking for, so it should be
"ul#latestResultsIndicator li:nth-child(2)"
Not sure whether that is going to solve your problem, but it's a place to start. If not, please update with the results

Related

Not able to click and element using Selenium

I am trying to automate 'Tableau' login screen and download reports flow. Everything works fine till I reach the 'Download' button. The first problem is I am NOT able to right click on the Download button and do an 'Inspect' in Chrome which is weird. I somehow am able to find the xpath by going through the whole page. When i try to click it I get "Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[2]/div/div[3]/div[4]/span[1]"}".
The code that I am using is mentioned below:
String xPath = "/html/body/div[2]/div[2]/div/div[3]/div[4]/span[1]";
(new WebDriverWait(driver, 20))
.until(ExpectedConditions.elementToBeClickable(By.xpath(xPath)));
driver.findElement(By.xpath(xPath)).click();
The page layout is like this from where I am picking the xpath
<div class="tabToolbarButton tab-widget" style="-webkit-user-select: none; -webkit-tap-highlight-color: transparent; width: 77.4px;">
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Download</span></div>
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Download</span>
</div>
Any help would be appreciated. What could be the reason for not able to 'Inspect'??
If you are not able to right click on the element, you can still right click anywhere on the page and select Inspect this element. You can then grab little binocular icon(tool tip message-select an element in the page to inspect it) and point it to the desired web element to check the xpath.
1st thing you need to check if you are writing xpath correctly. On a chrome you can do that without any plugin. On the developer window(same window which gets open when you select Inspect Element option), look for Console window and write your xpath as mentioned below and hit Enter.
$x("Write x path here")
for e.g. xpath for google search box
$x("//input[#id='lst-ib']")
I'm not sure why you writing absolute xpath. Its a bad practice. You can write x path for Download button in better way.
e.g.
(//span[#class='tabToolbarButtonText'])
I got the problem. So it seems the web page I was trying to automate has used frames. That is why even after providing the xpath the element was not getting clicked. I had to use 'switchTo.frame' in order to get the frame in focus and then I was able to click the element. Thanks for all the responses.
What I have still not understood is that why I was not able to inspect the element directly but that is a different question.

Using Element's tool tip as Locator in Selenium Webdriver

Can we use button's tool tip as a locator in selenium WebDriver? I came across this question because none of the default locators provided in selenium webdriver are useful for my applications elements.
Elements in the application i am testing randomly changes its,
Id,
Xpath-pos,
Xpath-relative Id, and
CSS
everytime the page is Refreshed! This happens because the application is created in ExtJs and HTML 5.
Yes, you can. But please show your HTML code. There are no general answers.
For example, this ExtJS demo here. http://try.sencha.com/extjs/4.1.1/demos/Ext.Button.tooltip.1/
<button id="button-1009-btnEl" type="button" class="x-btn-center" hidefocus="true" role="button" autocomplete="off" data-qtip="I'm a custom tooltip" style="height: 16px;">
<span id="button-1009-btnInnerEl" class="x-btn-inner" style="">Button w/ QuickTip (qtip) tooltip</span>
<span id="button-1009-btnIconEl" class="x-btn-icon "></span>
</button>
The tooltip is coded as data-qtip attribute. So you can use CSS Selector or XPath to find it.
XPath: //button[#data-qtip=\"I'm a custom tooltip\"]
CSS: button[data-qtip=\"I'm a custom tooltip\"]
Good question !
I would like to discuss it with some more experts but as far as my knowledge goes i dont think u can use tool tips as locators in webdriver!
Try this flow, may help you
don't bother if your script fails in IDE
Export it in webdriver/java/Junit
Try running the webdriver script now.
If the tooltip is in the HTML (which it must be), then you'll be able to use an XPath selector to find your element. Post a snippet of the HTML with the element you're trying to locate for more help

Runtime.evaluate threw exception: Error: element is not attached to the page document error when clicking on a element using watir-webdriver

HTML Tags
<li class="filter-categ">
<ul class="l-h-list">
<li id="filter__entityBased" class="item filter_value" filter-name="entityBased" search- name="zone/concept/store_based">Zone/Concept/Store based</li>
<li id="entityBased" class="sprite-filter all-filter"></li>
</ul>
</li>
I am trying to click on the element with id=entityBased, but I am getting the error."Error: element is not attached to the page document"
Same works fine in Firefox and IE. Problem is seen only in Chrome.
I am using the following code to perform the action.
browser.element(:id => "entityBased").wait_until_present
browser.element(:id => "entityBased").click
My Environment Details
Chrome Version 29.0.1547.66 m
Watir-WebDriver (0.6.4, 0.6.2)
watir (4.0.2 ruby x86-mingw32)
This is blockin my automation in Chrome. Please help me out in this problem.
I can't comment, sorry.
Have you tried testing with a 5 second sleep, instead of wait until present? From what I could tell, this generally occurs when the javascript reloads the element after the page has loaded.
user2783685
I had the same problem and I found solution in
stale element reference: element is not attached to the page document
So basically javascript reloading element and even I have this on your dom, driver thinking that it's detached.
and you need to catch org.openqa.selenium.StaleElementReferenceException
may be there is another way, I don't know.

Cannot find button element on form using Webdriver

I'm having trouble trying to click on on a button within a form.
I've tried xpath, cssselector, className, id, but still cannot find it.
Here's the HTML snippet for the button:
<input type="button" value="Continue" id="ni-reg-btn-register" class="btnNext ni-reg-btn-register">
I'm using WebDriver in Java
Getting this trace:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.09 seconds
Frequency: 100%
Browser: Firefox
URL: https://subscription.thetimes.co.uk/webjourney/webj_capturecustomerdetails
I've tried each of the following lines of code one by one (but no luck):
driver.findElement(By.className("btnNext ni-reg-btn-register")).click();
driver.findElement(By.cssSelector("buttons#ni-reg-btn-register.btnNext ni-reg-btn-register")).click();
List<WebElement> buttonlist= driver.findElements(By.className("btnNext ni-reg-btn-register"));
driver.findElement(By.id("ni-reg-btn-register")).click();
driver.findElement(By.xpath("//*[#id="ni-reg-btn-register"]")).click();
I was able to click the Continue Button using the below xpath.
//input[#id='ni-reg-btn-register']
I would suggest trying a WebDriverWait call to wait for the element in question to exist on the page prior to interacting with it. The Java documentation can be found here. It appears that while you are using the correct locator and your page is working, there is a timing issue where Selenium is trying to access the element prior to it being loaded.
EDIT: I was unable to locate an element on the provided page with the supplied id. I assume I am missing a step, but I did a search on the HTML for that page and found nothing.
I dont find the above button on the page link you provided ..
better chk the page link and try to give some wait statement after page load ...
implicit wait would be better or simpli Thread.sleep(2000);

Selenium locator for span loaded with AJAX

I'm new to Selenium IDE and can't find exact answer to my question elsewhere.
I have Login form that loads error message in span tag using AJAX.
Code looks like this:
<dd class="field-error-template atk-form-error">
<i class="ui-icon ui-icon-alert"></i>
<span class="field-error-text">Incorrect login information</span>
</dd>
I used following Selenium code to verify that this span appeared on screen:
waitForElementPresent css=dd.field-error-text span
However it returns false so any other ideas? I don't want to put delay as I don't think it is good practice.
Thanks,
Gita
You got your classes mixed up; the selector should either be this
dd.field-error-template span
Or this
dd span.field-error-text
(or use both classes)

Resources