Robot Framework Test Data Editor - Click on SPAN/Div not working - selenium

I wanted to perform click on Panel element in Selenium Robot Framework
code below:
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<span href="#panel" data-parent="#accordion" data-toggle="collapse" class="accordion-toggle panelTitle collapsed" id="panel" aria-expanded="false">Text 1<span class="toggle-icon"><i class="fa fa-plus-circle"></i></span>
</span>
</h4>
</div>
I want perform a click on "span" tag the whenever I write in my selenium robot framework as
Click Link (in first column) id=panel (in second column)
It doesn't work.
In my previous projects it was working fine but I am not able to make it work in this.

This is pretty hard to answer without you providing your test code or more detail but I suspect the element doesn't exist on the page when you try to click it? What error message are you getting? Providing this can help get an answer quicker.
Reading your question closer, are you using the right keyword, are you actually clicking a link? i.e. something contained in link tags? e.g.
all we've done together
Here's a rudimentary example:
Wait Until Page Contains Element panel 10
Click Element panel
This link should help you find further information if you require it: http://robotframework-seleniumlibrary.googlecode.com/hg/doc/SeleniumLibrary.html?r=2.8#Page Should Contain Element
Other libraries contain similar keywords you could use like the Selenium2Library

Related

Unable to acces Drop Down List using XPATH

I am new in selenium Web automation. I am trying to access the elements in a drop down menu but unable to do so. Below is the HTML part:
<div class="DropdownWidget---dropdown_value" id="3a5fa336ce7939555b5cd5d4fcb714fa_value" role="listbox" tabindex="0" aria-controls="3a5fa336ce7939555b5cd5d4fcb714fa_list" aria-expanded="false" aria-activedescendant="" aria-labelledby="3a5fa336ce7939555b5cd5d4fcb714fa" aria-describedby="3a5fa336ce7939555b5cd5d4fcb714fa_selectedChoice">
<!-- react-text: 15 -->Element1<!-- /react-text -->
<span id="3a5fa336ce7939555b5cd5d4fcb714fa_selectedChoice" class="DropdownWidget---accessibilityhidden">element1</span>
</div>
The list in drop down gets generated in the HTML when we click it. Xpath is unable to detect it.
Please help..
The best pattern is to convince a developer to add data-test-id="MyDropdownWidget" to DOM element that you want to test like:
<div data-test-id="MyDropdownWidget" ...
Then you can access it via driver.findElement(By.xpath("//#*[starts-with('MyDropdownWidget','data-test-id')...

span inside a button tag not clickable using selenium webdriver?

I'm trying to click and "Add Course" labelled button using selenium webdriver but it is not working out for me.
Here's the snippet I took from chrome developer tools:
<button type="button" class="btn btn-green" onclick="javascript:AddCourse();">
<span class="glyphicon glyphicon-plus-sign">
::before
</span>
<span translate="portallang_addCourse" class="open-sans ng-scope">
"Add Course"
</span>
</button>
You need one of the following xpaths:
First choice:
//span[contains(text(), 'Add Course')]
These two, only if there are always 2 spans and the second span is always Add Course:
(//button[#class='btn btn-green']/span)[2]
//button[#class='btn btn-green']/span[2]
Slowest option as the entire document is scanned. Add Course can only occur once on the page:
//*[contains(text(), 'Add Course')]
What error are you getting? What property did you use to click on the span element?
You got to use xpath for such elements.
Use xPath Finder add-on for Chrome and extract the unique xpath for this element:
https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en
You can always write an unique xpath manually but this just saves time and is accurate.
Hope this helps!

selenium click using span class or onclick

I am a newbie to java and selenium webdriver. I am having an issue clicking an image. Below is the page source.
<a href="javascript:void(0);">
<span class="HomeButton" onclick="javascript:onBtnHomeClick();"/>
</a>
I tried below codes but did not work and still getting the Unable to locate element error.
driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("HomeButton"))).click();
I have to click the homebutton. Any help would be much appreciated
I don't know why By.className("HomeButton") didn't work but you have errors in the other two.
In driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click(); the tag for onclick is <span> not <a>. It also not onBtnHomeClick() but javascript:onBtnHomeClick();
driver.findElement(By.xpath("//span[#onclick='javascript:onBtnHomeClick();']")).click();
If you want to use onBtnHomeClick() use contains
driver.findElement(By.xpath("//span[contains(#onclick, 'onBtnHomeClick')]")).click();
Or
driver.findElement(By.cssSelector("onclick*='onBtnHomeClick'")).click();
And in wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click(); the <span> parent tag is <a>, not <div>
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/a/span"))).click();
You simply need the correct locator IF your element will be eventually visible.
Xpath = "//span[contains(#class,'HomeButton') and contains(#onclick,'onBtnHomeClick')]"
Add wait as needed above exanmple, that should work.

Selenium Web Driver - How to narrow down my search using xpath? //* is not sufficient

Firstly thanks for your time.
I am trying to use selenium to find an entry in a listbox of my web app
driver.findElement(By.xpath("//*[text()='" + title + "']")).click();
How would I go about modifying my code to stop searching for everything and search for the following: (myXpath output on my Listbox from firebug/firepath)
<div id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList">
Thanks again, could you also explain as best as possible your answer, I'm trying to learn this as I will need it a lot going forward
<div id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList">
Displaying 1-10 of 47 Records
<div class="ListBox">
<div class="divListItem">
<a id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_lvSurveys_ctrl0_ctl00_btnApplicationID" class="manCensoredName" href="javascript:__doPostBack('ctl00$ctl00$MainContentPlaceHolder$MainContentPlaceHolderSurvey$lvSurveys$ctrl0$ctl00$btnApplicationID','')">11</a>
</div>
<div class="divListItem">
<div class="divListItem">
<div class="divListItem">
<div class="divListItem">
<div class="divListItem">
<div class="divListItem">
<div class="divListItem">
<div class="divListItem">
<div class="divListItem">
C# exemple to click on link:
driver.FindElement(By.Id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList")).Click();
For doing it in XPath:
driver.FindElement(By.XPath("//*[#id='ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList']")).Click();
I would write that one like:
driver.findElement(By.xpath("//div[contains(#id,'upSurveysList')].click();
No need to write the entire id;
Using the "contains" method of selecting xpaths, you can take any partial text out of the div to match it (as long as that text doesn't appear elsewhere in the html, but in your case, I don't think it would).
I shy away from using //* at all times, for it searches through absolutely everything. Depending on the size of your web application, if you know you're accessing a div or a canvas or an img, you can reduce your test times as well as increase xpath accuracy and maintainability.
Also, I know personally, my webapplication is extremely finicky, and the use of Actions and WebElement is the only way to click on the majority of my buttons.
For example:
WebElement mybutton = driver.findElement(By.xpath("//div[contains(#id,'upSurveysList')].click();
Actions actions = new Actions(driver);
actions.moveToElement(mybutton).build().perform();
mybutton.click();
Let me know if that works for you.

Selenium webdriver script that runs in IE hangs in Firefox

I have used selenium RC in the past but I am new to webdriver.
I have three links in my app. Notifications, messages and connections.
On clicking notifications, notifications drop box is displayed. On clicking messages, messages drop box is displayed and on clicking connections, connections drop box is displayed.
In the script, I click notifications link, wait for notifications drop box and then assert for notifications dropbox. Same for messages and connections in sequence.
The notification sequence works correctly. In messages, it clicks on messages link and then hangs on the wait for messages drop box link. I know this since I had put print command after every line.
Heres is my code:
driver.findElement(By.xpath("//a[#id='notifications-page-button']")).sendKeys("\n");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#id='notifications-dropdown-list']//li//div[#class='message']")));
Assert.assertTrue(isElementPresent(By.xpath("//div[#id='notifications-dropdown-list']//li//div[#class='message']")), "Notifications drop box was not displayed");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[#id='messages-page-button']")));
driver.findElement(By.xpath("//a[#id='messages-page-button']")).sendKeys("\n");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#class='sf_messages_list dropdown']//li//div[#class='message']"))); //This is the line where the script hangs. If I remove this line and the next line and continue with just the click commands, they work. But when I have this line and the next, the remaining click commands are not executed
Assert.assertTrue(isElementPresent(By.xpath("//div[#class='sf_messages_list dropdown']//li//div[#class='message']")), "Messages drop box was not displayed");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("connections-page-button")));
driver.findElement(By.id("connections-page-button")).click()
Heres the HTML for messages section:
<li class="icon mm open">
<a id="messages-page-button" class="mm" href="#">
<span class="icon"></span>
<span class="badge hidden_elem">
<strong>
<em>0</em>
</strong>
</span>
</a>
<div class="dropdown-holder">
<div class="sf_messages_list dropdown" data-eventprefix="mainmenu_messages">
<div class="tb-dropdown-header">Messages</div>
<div class="tb-dropdown-body" data-url="/messages/dropdown">
<div class="document-title" style="display: none; !important"> - Dropdown Messages</div>
<div id="messages_list_view_76345" class="message-threads-listview">
<ul>
<div class="no_items hidden_elem">
</div>
</div>
<div class="tb-dropdown-footer" style="display: block;">
</div>
<span class="shadow-hide"></span>
</div>
</li>
Heres the HTML for notifications section:
<li class="icon mm">
<a id="notifications-page-button" class="mm" href="#">
<span class="icon"></span>
<span class="badge hidden_elem">
<strong>
<em>0</em>
</strong>
</span>
</a>
<div class="dropdown-holder">
<div id="notifications-dropdown-list" class="sf_notifications_list dropdown" data-eventprefix="mainmenu_notifications">
<div class="tb-dropdown-header">Notifications</div>
<div class="tb-dropdown-body" data-url="/notifications/dropdown"></div>
<div class="tb-dropdown-footer">
<a class="view_all" href="/notifications/view">View All Notifications</a>
</div>
</div>
<span class="shadow-hide"></span>
</div>
</li>
The above code works on IE. So looks like the problem is not that it is not able to find the element.
I am using Selenium 2.25.0. I have tried various versions of FF including 3.6, 7, 11, 13, 15 and 16. But none of them worked.
Also, the script just hangs. It does not even throw an error in eclipse. I had once let my script run for around 11 hours and still no error.
Please let me know in case you need further information to help me resolve this issue.
Thanks!
Something similar happened to me in previous versions of Selenium Webdriver. And I was also clueless, whats happening to me. Eventually, updating to newest version of Selenium helped me for good, but because 2.25.0 is the latest, I will at least present you the workaround I was using until update solved it
Whenever I needed to click a button, nothing happened (as to you). So the workaround was, that when clicking button, I also send Enter key event to it.
To be more specific:
WebElement buttonWhereClickingDoesNotWork = driver.findElement(By.id("messages-page-button");
buttonWhereClickingDoesNotWork.click();
buttonWhereClickingDoesNotWork.sendKeys(Keys.ENTER);
Yes, its workaround. Yes, its not nice. And yes, it did help me.
And also: No, I do not know the root cause of this, since update of Selenium helped me in my case...
This seems to have some connection with a Facebook Login. Are you correctly switching between window handles? Also what is your implicit time out, by default it is set at 30 seconds, so I am not sure if your script can run for 11 hours without erroring out.
Can you try these
1) I am guessing, className("message") does not exist, and your script is actually getting stuck at that step and not the step after. Which involves the click.
driver.findElement(By.id("notifications-page-button")).click();
wait.until(driver.findElement(By.id("messages-page-button")));#<<-- Changed the element to wait for
//The code works untill here. But on the next command that clicks messages link, it hangs
driver.findElement(By.id("messages-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("username"))));
2) Remove that wait for element
driver.findElement(By.id("notifications-page-button")).click();
#removed this wait for element
//The code works untill here. But on the next command that clicks messages link, it hangs
driver.findElement(By.id("messages-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("username"))));
UPDATE
Please try this ...
driver.findElement(By.id("notifications-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("message"))));
//The code works untill here. But on the next command that clicks messages link, it hangs
driver.findElement(By.id("messages-page-button")).click();
wait.until(driver.findElement(By.id("connections-page-button"))); # changed it to wait for the element that you will next work with
driver.findElement(By.id("connections-page-button")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.className(("connections-listview"))));
Also, if your provide an HTML snippet of the web elements, which you are not sure abt the locator, we can help figure it out.