I'm trying to select an element I'm working with: - javascript

So, written code looks like this:
IWebElement Enter = webDriver.FindElement(By.LinkText("Enter"));
Im trying to get grab a Enter txt from code:
<li>
<a onclick="javascript:var p;if (document.getElementById('chckPot').checked==true){p=1;}else{p=0;};waitScreen();window.open('frmnewname.aspx?mod=0&pot='+p,'mojframe');" href="#">
<i class="fa fa-lg fa-folder-open">;</i>
Enter
</a>
</li>
So what ever I do I get the following error-message :
OpenQA.Selenium.NoSuchElementException: Unable to find element with LinkText == Enter
Code for test is written in C#, and previous version of app has a ID but new one don't have it (look in code).

Partial link text may help here (there could be extra newlines and spaces):
IWebElement Enter = webDriver.FindElement(By.PartialLinkText("Enter"));

Here is how you would approach this using xpath:
IWebElement element = webDriver.FindElement(By.XPath("//a[contains(text(), 'Enter')]"));

Related

How to read the html tags innerHTML value using css selector or xpath.?

My actual query is, I want to retrieve the HTML inner text value using css selector or xpath. I am able to acheive this by using document.getElementById but not using selectors, instead I can only able to print the tag element but not the text from it.
For Ex:
<li class="active">
<span id="lastPrice">1,603.35</span>
<span id="CAToday"></span><br>
<span class="up" id="change">28.80</span>
</li>
From the above HTML, I want to print 1,603.35 using either
Css or
xpath
Note: I have drilled the forum already but couldn't able to find required solution.
This XPath expression
string(/li/span[#id='lastPrice'])
With this well-formed XML
<li class="active">
<span id="lastPrice">1,603.35</span>
<span id="CAToday"></span><br/>
<span class="up" id="change">28.80</span>
</li>
Result
1,603.35
Check in http://www.utilities-online.info/xpath/?save=07d6e884-4f7e-46cc-9aaf-904e6a440f50-xpath
You should be able to use the XPath version 2 matches function:
//div[matches(text(), 'Hello ?\w+ What would you like to do today \w+')]
which does allow regular expressions.
Java Selenium (xpath example):
driver.findElement(By.xpath("//span[#id='lastPrice']").getAttribute("innerText")
Python Selenium (CSS example):
driver.find_element_by_css_selector("span#lastPrice").get_attribute("innerText")

Selenium get element from menu without ID

I am working with Selenium Web driver and need to reference am item in a javascript menu that has no id. I need to know how I can get the following web element in Selenium. I am not very proficient with javascript or HTML so any help is needed.
<td class="menu" colspan="2">
<a href="javascript:Redirect('marks',0);" class="menu">
Display Text Here
</a>
</td>
To get the element with text as Display Text Here you can use either of the following Locator Strategies:
LinkText:
"Display Text Here"
CssSelector:
"td.menu>a.menu"
XPath:
"//td[#class='menu']/a[#class='menu'][contains(.,'Display Text Here')]"
You can locate this element by xPath:
//a[contains(., 'Display Text Here')]
so in JavaScript it will be like this:
driver.findElement(By.xpath("//a[contains(., 'Display Text Here')]")).click();
Here you will find more information.
You can use this linkText :
Note that you should always choose linkText when compared to xpath and cssSelector.
driver.findElement(By.linkText("Display Text Here")).click();

New Line character in title attribute does not work in tooltip

I have an html form where I need to add new line character in to the title attribute of the anchor tag. I found a few solutions online but none of them work in my code.
I tried adding
\n
<br>
not sure what is missing. Is it because of data-toggle that is blocking from breaking the title attribute?
$(document).ready(function () {
$('form').attr('novalidate', true);
$('[data-toggle="tooltip"]').tooltip();
<a class="helptext fa fa-question-circle" data-toggle="tooltip" title="Additional information -
 You can provide further information here to for your
 application to issue a new card.
 Ensure a maximum of 200 characters
 is entered – including spaces."></a>
Setting the title with JavaScript works for me.
document.getElementById('asdf').title = 'line1\nline2';
A popover would be a better solution, but it is possible to achieve this functionality in browser:
$(document).ready(function () {
$('form').attr('novalidate', true);
//$('[data-toggle="tooltip"]').tooltip();
$('.helptext')[0].title = "I like\nlines"
// you can also use $('.helptext').attr("title",decodeURI("I like%0A to%0Ause%0Alines"))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="helptext fa fa-question-circle" data-toggle="tooltip" href="#" title="Additional information -
 You can provide further information here to for your
 application to issue a new card.
 Ensure a maximum of 200 characters
 is entered – including spaces.">test</a>

click on hyperlinks in unit testing using protactor in angularjs

I am new in unit testing in angularjs. I read the tutorial from:
https://github.com/angular/protractor/blob/master/docs/tutorial.md.
I want to click on hyperlink but i dont know how can i achieve this.
Here is my code:
<li ng-repeat="menu in sidebarLinks" ng-if="menu.visible == true " ng-class="{active: isActive('/{{menu.action}}')}">
<a ng-href="#/{{menu.action}}" title="{{menu.name}}" ng-click="loadSubmenus(menu.action)" ng-class="{active: isActive('/{{menu.action}}')}">
<div class="icon {{menu.icon}}" ng-class="{active: isActive('/{{menu.action}}')}"></div>
<span>{{menu.name}} </span>
</a>
</li>
In unit testing:
describe('Protractor Demo App', function() {
browser.driver.get('https://localhost:8443/login.html');
browser.driver.findElement(by.id('name')).sendKeys('test');
browser.driver.findElement(by.id('password')).sendKeys('test');
browser.driver.findElement(by.id('login')).click();
});
After login url is https://localhost:8443/#/dash
After click on link url should be https://localhost:8443/#/hypera
Since your links won't work for anyone else because you're running it locally on your machine, I can only tell you the best way to do it.
If you're dealing with hyperlinks, you need to get the element that you've set with 'login'. I'm not seeing it in your HTML snippet so I can't give you the correct path.
If it was a basic hyperlink, you could find it easily by using something like:
element(by.linkText('login')).click()
If your login button's text is indeed 'login'. If it is something else, like a button, you'll need to find the exact element and it will look like:
element(by.css('span[class="login"]')).click()
If your login button is under a 'span' tag in your HTML page.

How to resolve NoSuchElement exception in selenium?

I had one link initially. Now I received the requirement of showing button instead of a link for A/B testing. I have to make their ids same since I don't want to write a new test for the button.
To decide which to display I added two unique classes in the li item. This is working fine.
But on testing it starts failing, giving the message "Subscriber link is not available" although button is present there.
I thought that it might failing because of two same ids are there so i added two same classes and change the selector from id to class. But it still failing.
<li class="not_subscriber subscriber-text-link " style="display:none">
<a class="link-orange subscribe" href="" id="subscribe_link"> Subscribe </a>
</li>
<li class="not_subscriber subscriber-orange-btn " style="display:none">
<a class="btn-orange subscribe" href="" id="subscribe_link" > Subscribe </a>
</li>
#FindBy(css = ".subscribe")
#NoSuchElementDescription("Subscriber link is not available")
protected WebElement _subscribeLink;
Is there any way such that i dont have to write new test and change ids and test will start passing??
Can you please try below Xpath:-
//li[#class='not_subscriber subscriber-orange-btn ']/a[#class='btn-orange subscribe']
Hope it will help you :)
Change the selector to xpath. Almost every element has an xpath. And be sure to replace the double quotes to single quotes. It should be more reliable than class or id selectors.

Categories