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

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?

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()

click on submit button not working in selenium webdriver

I trying to click on the create account button in registration form.
this is how the button locate in the html page:
<div id="submitContainer"><button type="submit" class="large"><span><strong> Create Account </strong></span></button></div>
this is the button xpath:
//*[#id="submitContainer"]/button/span/strong
the problem is that the button don't have id, he locate inside a div.
I try to use by id,xpath,css,name, but all of this not working:
driver.findElement(By.id("submitContainer")).click();
driver.findElement(By.xpath("//*[#id='submitContainer']/button/span/strong")).click();
driver.findElement(By.tagName("Create Account")).click();
driver.findElement(By.className("large")).click();
thanks!
In your examples, except for the last one, you are not targeting the button. Now your last example, should actually locate the button-element:
driver.findElement(By.className("large")).click();
Could you please post the error message you are getting?
Are there more than one element on the page with className "large"?
Make sure the button is in view window, if it is then try clicking on it. Try to wait for the element to load. There might be an issue with your element being loaded into DOM -
driver.wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[#type='submit']"))).click();
Hope this helps.
If you want to use xpath, the correct syntax is
//button[#type='submit']
Use this line below:
Thread.sleep(3000);
I got the result once I used this one. Since some time we need to give some sleep time for the site to load fully to pull the Xpath.
You can use the linkText
driver.findElement(By.linkText("Create Account")).click();
Hope it will work for you.

selenium code for accessing button type submit

i want to access the submit button having code
<input class="btn btn-primary" type="submit" value="Save">
I have tried for ;
driver.findElement(By.name("Save")).click();
and
driver.findElement(By.name("Save")).submit();
but raising an error of : No such element Unable to locate Element
Your element has no name, it has a tag and some class names which may or may not be unique across the page you are testing. These may work but we might need to see more of the page.
driver.findElement(By.className("btn-primary")).click();
driver.findElement(By.cssSelector("btn.btn-primary")).click();
You may find the page http://docs.seleniumhq.org/docs/03_webdriver.jsp useful as it gives you an introduction to the different ways to find elements on the page.
Add a sleep time or wait before the submit.
Thread.sleep(5000);
driver.findElement(By.name("Save")).click();
I think this will solve your problem

click # checkbox is done but not registered

I`m relatively new to Selenium and trying to create some test cases with the Selenium IDE.
I just selected a checkbox with XPath with the following command xpath=//input[starts-with(#id,'003g')].
After the checkbox is selected, I click another button to add the value from the checkbox.
But nothing happens. The application always tells me that no record is selected... If I push the play button I can see IT IS selected.
What to do?
Thank you guys!
edit:
At first... Thanks for your help!
I don`t think, the problem is caused due the fact that the element is not present. I can see that Selenium selects the according data record (checkbox)
Here is some code. I hope it will help! In fact I am new to this whole webstuff and have no clue about web techniques.
<td class="x-grid3-col x-grid3-cell x-grid3-td-checkbox x-grid3-cell-first
"tabindex="0" style="width:18px;">
<div id="003g0000006ElPO_checkbox" class="x-grid3-cell-inner x-grid3-col-checkbox">
<input id="003g0000006ElPO" class="checkbox" type="checkbox" name="ids" title="<a
href="/003g0000006ElPO">
<span>JeanneSeleniumOne</span></a>" value="003g0000006ElPO">
There's too little information to help, but a wild-guess would be the element wasn't ready to be clicked i.e. you should wait for some time before clicking.
I just posted a similar answer here

Webdriver unable to find element

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