Webdriver unable to find element - selenium

I was unable to find the element (with id below) with Selenium even though it's visible in the html source page after successfully clicking on 'Search' button (using Selenium) of the previous page that has url as follows:
String url="https://sjobs.brassring.com/1033/ASP/TG/cim_advsearch.asp?partnerid=25314&siteid=5290";
driver.get(url);
if(driver.findElements(By.id("submit1")).size() != 0)
driver.findElement(By.id("submit1")).click(); // clicking on 'Search' button
if(driver.findElements(By.id("ctl00_MainContent_GridFormatter_YUIGrid")).size() != 0)
System.out.println("FOUND!");
String pageSource= driver.getPageSource();
"FOUND!" was never rendered, nor pageSource contained the element with the above id. I am using Selenium 2.3.3 and testing with latest versions of IE, Chrome, and Firefox webdrivers. Could someone please help? Thank you.
About 1/3 from the bottom of the target page are the followings (third line is location of the id):
<div id="ctl00_MainContent_GridFormatter_datatable" class="datatable">
<div id="THeadersDiv" style="display:none;">
<table id="ctl00_MainContent_GridFormatter_YUIGrid" class="basicGrid" border="0"> <!-- this is the element in question -->

I think I got it. I believe that the driver cannot find the element because there are two elements both with identical IDs. (which is terrible web code). I looked at the rest of the code, and it looks like the two elements also share the same class, and are the only two elements with that class.
Therefore, I believe that doing a By.className(".basicGrid") should work

Related

Using Selenium with Java on Salesforce Lightning / Javascript problems

We had Selenium tests running on Sales Force non Lightning. Now I am converting to Lightning. There is a chain of menus to go through. I have successfully navigated to a certain menu, but now I have run into a problem. This is not the standard question about new element names. Here is the question. This is a small piece of what the page looks like (see below).
When using inspect with Chrome I can find the elements. This is what it looks like (a small cross section corresponding to above). The SPAN tag has the label ("Original Agreement") and the DIV below it will contain the box for the input value.
<div class="slds-form-element__control" data-aura-rendered-by="228:1224;a">
<div data-aura-rendered-by="1108:0" class="uiInput forceSearchInputLookupDesktop uiInput--default" data-aura-class="uiInput forceSearchInputLookupDesktop uiInput--default">
<label class="label inputLabel uiLabel-left form-element__label uiLabel" for="157:1224;a" data-aura-rendered-by="1103:0" data-aura-class="uiLabel">
<span class="" data-aura-rendered-by="1104:0">Original Agreement</span>
<!--render facet: 1106:0--><!--render facet: 1107:0--></label>
<div data-aura-rendered-by="161:1224;a"><div class="contentWrapper slds-box--border" data-aura-rendered-by="162:1224;a">
However, Selenium can not find the elements (though inspect does). When I did a "View Page Source instead of an
inspect, almost the entire thing is in JavaScript like this:
function rewriteAndInjectCss(linkEl, source, varLookup) {
var css = rewriteCssVars(source, varLookup);
injectStyles(linkEl, css);
}
but a lot more. Almost all functions, with maybe only a couple elements.
In the past when I have seen something similar there is often an iframe to switch to to get the elements. But there is no iframe to switch to. So I am stuck how to get these. Can anyone shed some light?
OK. Found it. I needed to do a driver.switchTo().defaultContent()

Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver

Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver
In my Ajax application, we have dropdown + link (Customize) together in div and i want to click to Customize link. I have locator and which was working fine for Customize link with old selenium but it doesn't with latest web driver. Can anyone please point me the problem or suggest something to make it work?
Expected:
Clicking to Customize link should open respected option (it actually opens dialog).
Actual:
Below locator clicks to dropdown button instead of Customize link due to such a complex page DOM which has no actual href or anchor tag.
Locator:
css=div[id$='_repeatDesc'][class='FakeAnchor']
Html:
<div id="zcs1_repeatDesc" class="FakeAnchor" style="cursor: pointer;">Customize</div>
Code:
webDriver().findElement(By.cssSelector("div[id$='_repeatDesc'][class='FakeAnchor']")).click();
I think your locator is not unique, may be it is locating dropdown element that's why it clicks to dropdown button instead of Customize link.
You should try using By.xpath() with text() node to locate this element as below :-
webDriver().findElement(By.xpath(".//div[text() = 'Customize']")).click();
Or As I'm seeing in HTML element has id attribute, if it's unique I'd to locate desire element and it's not being changed dynamically, you can try also using By.id() as below :-
webDriver().findElement(By.id("zcs1_repeatDesc")).click();
Edited :- If you want to click using JavascriptExecutor try as below :-
((JavascriptExecutor)driver).executeScript("arguments[0].click()", webDriver().findElement(By.xpath(".//div[text() = 'Customize']")));

How to get Inspect Element code using Selenium WebDriver

I'm working in selenium with Firefox browser.
The Html code shown in View Source (CTRL+U) is different from the html code i see when inspecting the elements in Firefox.
When i run the driver.getPageSource() i only get the View source (CTRL + U) codes.
Is there is any way to access the Inspect element code instead of View source code?
I think your question is answered here.
The View Source html is what is sent by the server. I think of it as compile time html, or the initial state of the DOM.
The Inspect Element html could have been updated by ajax responses or javascript so will not necessarily be the same. I think of it as runtime html, or the current state of the DOM.
The GetAttribute() method queries the current DOM element state. You can return a particular html attribute value directly
webElement.GetAttribute("class")
or get the whole html string.
webElement.GetAttribute("innerHTML")
There are some fundamental difference between the markup shown through View Source i.e. using ctrl + U and the markup shown through Inspector i.e. using ctrl + shift + I.
Both the methods are two different browser features which allows users to look at the HTML of the webpage. However, the main difference is the View Source shows the HTML that was delivered from the web server (application server) to the browser. Where as, Inspect element is a Developer Tool e.g. Chrome DevTools to look at the state of the DOM Tree after the browser has applied its error correction and after any Javascript have manipulated the DOM. Some of those activities may include:
HTML error correction by the browser
HTML normalization by the browser
DOM manipulation by Javascript
In short, using View Source you will observe the Javascript but not the HTML. The HTML errors may get corrected in the Inspect Elements tool. As an example:
With in View Source you may observe:
<h1>The title</h2>
Whereas through Inspect Element that would have corrected as:
<h1>The title</h1>
getPageSource() always returns the markup obtained through View Source.

Selenium webdriver : Unable to click a link (Not inside iframe- able to print gettext value from the webelement)

Please don't make comments regarding why i posted a similar question. I have tried many things and nothing is working. Below is the HTML
<div id="businessSettingsColumn1">
<div class="sectionLink">
Business details
</div>
<div class="sectionLink">
Operating hours
</div>
<div class="sectionLink">
Closed dates
</div>
<div class="sectionLink">
Appointment notifications
</div>
I need to click the second link
I have tried
1) webdriver.findElement(By.partialLinkText("Operating hours")).click();
2)webDriver.get(mylement.findElement(By.tagName("a")).getAttribute("href"));
3)
List<WebElement> businessLinks= busCol.findElements(By.className("sectionLink"));
for(WebElement bLink :businessLinks) {
if(bLink.getText().contains("Operating hours")) {
bLink.findElement(By.tagName("a")).click();
}
}
4) Using the Action builder to move the mouse and then doing a click
Also when i did this 3 times in a row , my element got clicked
webdriver.findElement(By.partialLinkText("Operating hours")).click();
webdriver.findElement(By.partialLinkText("Operating hours")).click();
webdriver.findElement(By.partialLinkText("Operating hours")).click();
I am using Firefox version 25.0 and Selenium version 2.35.0. Funny thing though is when i do a sysout , the values get printed and when i try to get the url using webdriver I get "Element not found in the cache - perhaps the page has changed since it was looked up" .. its pretty much a static page with links only so i dont understand why i am not able to click it.. Any help will be much appreciated.
Can you try calling focus() on the element before you click on it?
This usually occurs because the element was there at some point, but then something happened and then it's no longer there for some reason. I encounter this frequently when a page makes AJAX calls for example.
Have you tried adding some waits so that selenium is sure that the element is ready?

How to click on <input type=file> across browsers using Selenium Webdriver?

I'm working on dealing with a file-chooser dialog using Selenium 2 - WebDriver. Believe it or not, my problem is NOT dealing with the OS-native file-chooser. That part I can handle!
The problem is getting Selenium to properly click on the "Choose File" button. Since the original source html is simply <input type='file'>, the browser determines how to render it as a field and a button. As a result, the placement and naming of the button changes depending on browser. I've got it working in Chrome, but only because Chrome places the button on the leftmost alignment and Selenium happens to click there by default.
Any ideas? It's not clear to me if an input of this type is truly navigable from within the DOM anyway...
The proper way to upload a file on any OS is to
Find the <input type='file'> element. You need not to worry about different implementations and exact positioning. Just find the element for example by xpath //input[#type='file']
sendKeys() or type() (or whatever method writes text into elements in your language) the path to file to that input element.
Sample Java code:
// find the input element
WebElement elem = driver.findElement(By.xpath("//input[#type='file']"));
// 'type' the file location to it as it were a usual <input type='text' /> element
elem.sendKeys("C://path/To/File.jpg");
This works on every OS and browser in WebDriver.
Have exactly the same situation with element <input type='file'>. In my case it is created using ExtJS.
I don't know whether you have solved this question or not but let me provide my solution.
JavascriptExecutor executor = (JavascriptExecutor)getDriver();
executor.executeScript("arguments[0].click();", element);
Neither sendKeys() or type() nor using ActionBuilder was helpful for me. The only JavascriptExecutor works like a charm.
I tested with the following element:
<INPUT style="WIDTH: 550px; background-color:yellow" type="file">
Results:
IE: doubleclick on any area of the element, the "Choose File" dialogue would occur;
Firefox: click on any area of the element, the "Choose File" dialogue would occur.