Unable to acces Drop Down List using XPATH - selenium

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')...

Related

Not able to find xpath. Chropath message - This element might be inside iframe from different src. Currently ChroPath doesn't support for them

Below is the HTML snippet I am trying to get xpath. I have tried so many combination in chrome dev tools but no luck. Chropath message is that element might be from different src.
I am not sure what it means. Any help appreciated.
<input type="text" class="sn-global-typeahead-input -global-active-input" name="sncwsgs-
typeahead-input" id="sncwsgs-typeahead-input" placeholder="Search Global" autocomplete="off"
aria-label="Search" aria-hidden="false" aria-expanded="true" aria-activedescendant="sncwsgs-
typeahead-record-1-2" aria-autocomplete="both" aria-owns="sncwsgs-typeahead-sections" aria-
controls="sncwsgs-typeahead-sections" aria-describedby="sncwsgs-typeahead-instructions" aria-
haspopup="listbox" value="" role="combobox">
There's an id tag, so you can use the following for the XPath:
"//input[#id='sncwsgs-typeahead-input']"
That also translates to the following CSS Selector:
"input#sncwsgs-typeahead-input"
But if that's inside an iframe, you'll need to switch to the iframe first before you can interact with elements inside of it.
Learn about switching into iframes here: https://www.selenium.dev/documentation/webdriver/interactions/frames/
Example of switching into an iframe:
driver.switch_to.frame("iframe")

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!

Aurelia eating my Bookmarks

I am working on a legacy application that is being rewritten using Aurelia, the application has a bunch of static html in a tblHelp that needs to be displayed. I am using innerhtml.bind on a div in my view to databind the stored static HTML into the view. Each record is essentially a document complete with a full table of contents that links to other divs within the document (Bookmarks).
Something like:
<div id="toc">
<h1>Table of Contents</h1>
<ul>
<li>Section 1<li>
<li>Section 2<li>
</ul>
</div>
<div id="section1">
<h2>Section 1</h2>
<p>Paragraph Text...</p>
<p>Back to Table of Contents</p>
</div>
<div id="section2">
<h2>Section 2</h2>
<p>Paragraph Text...</p>
<p>Back to Table of Contents</p>
</div>
When I display the resulting page in my Aurelia view and click on the links, rather than moving to the proper Div on the current page, it seems to be attempting to route to an unknown route and ends up returning to the home page (that is my unknown route behavior). How do I make the Aurelia Router know that I am just moving around the same page and do not require it to route to another page?
I think you need to change your <div id= to <a id= which is the correct syntax for anchors. Hopefully Aurelia will recognize them as legitimate anchors when formatted correctly.
Also, since an anchor tag shouldn't wrap the whole content, you'll just open and close it at the top of the div. You can even leave the divs there but should not duplicate the id.
Update:
That being said, I created a GistRun that actually demonstrates that Aurelia should be able to handle the <div id= anchor targets. So, I'm not exactly sure why you're having problems.
Maybe this GistRun or the more standard <a id= approach will help you.

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

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

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.