Selenium IDE: How to assert attribute exists - selenium

I'm using Selenium IDE.
I know how to test if an element's attribute has a certain value. But how do I test if the attribute exists in the first place?
Here is the line which successfully tests if the attribute has a certain value:
<tr>
<td>assertAttribute</td>
<td>id=_ctl0_MainPlaceHolder_dgMemberList_DXSelBtn0#disabled</td>
<td>disabled</td>
</tr>
Here is the line which unsuccessfully tests if the attribute exists in the first place:
<tr>
<td>assertElementPresent</td>
<td>id=_ctl0_MainPlaceHolder_dgMemberList_DXSelBtn0#disabled</td>
<td></td>
</tr>
How do I get it to work?
Thank,
-Ilya

You can use the "getAttribute" method, and if it returns "null" it means that there is no attribute at all...
From Selenium documentation:
WebElement.getAttribute( attributeName ) → Thenable<(string|null)>
Schedules a command to query for the value of the given attribute of the element. Will return the current value, even if it has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned. If neither value is set, null is returned (for example, the "value" property of a textarea element). The "style" attribute is converted as best can be to a text representation with a trailing semi-colon.
For example:
HTML code:
<div id="a" >1</div>
<div id="a" x="xxx" >2</div>
<div id="a" x >3</div>
Test code in Nodejs:
var webdriver = require('selenium-webdriver');
var capabilitiesObj = {
'browserName' : 'firefox'
};
var driver = new webdriver.Builder().withCapabilities(capabilitiesObj).build();
driver.get('http://localhost/customer.js/temp/index60.html');
driver.findElements(webdriver.By.css('div[id=a]')).then((elements) => {
elements[0].getAttribute('x').then((att)=> {
console.log(att);
});
}).catch(console.log.bind(console));
driver.quit();
The result for each div will be different:
for div 1: null
for div 2: 'xxx'
for div 3: ''

I don't think the simple id locator handles the attribute. So switch to xpath:
This should work:
<tr>
<td>verifyElementPresent</td>
<td>xpath=//.[#id='id3' and #attribute1]</td>
<td></td>
</tr>
Note that if you change the specific id (id1, id2, id3) it will pass for the first 2 ids but fail for the 3rd:
<div id="id1" attribute1="xyz" style="background-color: transparent;">Some text</div>
<div id="id2" attribute1="" style="background-color: transparent;">Some text</div>
<div id="id3" style="background-color: transparent;">Some text</div>

Related

BeautifulSoup returning 'None' when trying to find value of input

I have been attempting to use BS to find the value of an input field on a webpage. However, no matter what I try, it always returns as 'None' although the element certainly exists. Here is a sample of the HTML.
<td class="formArea"><table border="0" cellspacing="2" cellpadding="2">
<tr>
<td class="main">Street Address:</td>
<td class="main">
<input type="text" name="entry_street_address" id="entry_street_address" value="1234 Example Ln" maxlength="64" required> <span class="fieldRequired">* Required</span></td>
So I attempt to use BS4 to grab the value of 'entry_street_address':
r = session.get("sampleurl.com/wheremydataisstored")
time.sleep(3)
soup = bs4(r.content,'html5lib')
info = soup.find('input', {'id': 'entry_street_address'}).get('value')
print(info)
Unfortunately, this always returns:
AttributeError: 'NoneType' object has no attribute 'get'
This always happens. No matter if I do html5lib, lxml, html.parser, no matter how long I .sleep() to wait for the page to load, etc. I'm not really sure where it's going wrong!

Click on specific button based on name of the class or elements

I captured the testName by using driver.find_element_by_xpath("//td[contains(.,'%s')]" % test_name)
How can I capture class name based on testName?
I am trying to click on menu-button if it contains specific test name
<tr class="row-1">
<td>testName</td>
<td>testDes</td>
<td class="menu">
<div class="menu-button">
</div>
</td>
</tr>
This is one possible XPath expression :
//tr[td[contains(.,'testName')]]/td[#class='menu']/div[#class='menu-button']
Basically the above XPath locate tr element having child td value equals "testName", then return the corresponding div[#class='menu-button'] element.

How to retrieve the text from an outer element using selenium webdriver?

How to retrieve the text 'caught jake' from the below code using selenium webdriver?
Am able to point to that text using the below xpath but am unable to print the text. :(
//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()
<div class="row">
<div class="large 20 columns">
<table class="batting-table innings" width="100%">
<tbody>
<tr class="tr-heading">
<tr>
<tr class="dismissal-detail" style="display: table-row;">
<td width="2%" />
<td colspan="8">
<b>12.6</b> caught Jake
<b>73/4</b>
<br/>
Using java you can retrieve from following lines:
String text = driver.findElement(By.xpath("//div[#class='large 20 columns']")).getText();
System.Out.print("Text= "+text);
Above line will return all inner tags string's of div element.
If you want to get only 12.6 text, then use below lines:
String text = driver.findElement(By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")).getText();
System.Out.print("Text= "+text);
And be sure that there should be single element which has this xpath 'By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")', otherwise you will get an exception.
depend on your language, you can use following xpath expression:
//div[#class='large 20 columns']/table/tbody/tr/tr/td[2]/child::text()
full Java code:
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsx = "return document.evaluate(\"//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()\", document, null, XPathResult.STRING_TYPE, null).stringValue;";
String objList = js.executeScript(jsx);
System.out.println( (String) objList);

Selenium webdriver-java: how to get javascript tooltip

I want to get tooltip information enabling on JavaScript. I have tried with below code but its shown null every time.
Java Code:
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"));
action.moveToElement(element).build().perform();
System.out.println(driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a")).getAttribute("data-original-title"));
HTML Code is:
<tr class="single" name="Tasks_59c777d9-8d16-694a-7307-52caad36d751">
<td>
</td>
<td data-type="custom_task_name">
<span class="list" sfuuid="3840" data-original-title="">
<div class="ellipsis_inline" data-original-title="">
Task Testing
<br/>
Toney Harber
</div>
</span>
</td>
I have tried with span tag but this also shows null.
I am providing you 2 options try both.
Option 1
Please try below. It will return you a list. Loop through it. If there is only one span then you can get the element and then get the attribute value.
driver.findElements(By.xpath("//span[#data-original-title]"))
Option 2
Instead of
driver.findElement(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"))
use findElements
driver.findElements(By.xpath("//*[#id='content']/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/table/tbody/tr[1]/td[2]/span/div/a"))
It will return a list of web elements. Loop thorough it you might get more than one element.

I need to find an element in Selenium by CSS

I want to find the element of this link "us states" in <h5>. I am trying this on Craigslist. How can I do it?
Here is the URL: http://auburn.craigslist.org/
<html class="">
<head>
<body class="homepage w1024 list">
<script type="text/javascript">
<article id="pagecontainer">
<section class="body">
<table id="container" cellspacing="0" cellpadding="0"
<tbody>
<tr>
<td id="leftbar">
<td id="center">
<td id="rightbar">
<ul class="menu collapsible">
<li class="expand s">
<li class="s">
<li class="s">
<h5 class="ban hot">us states</h5>
<ul class="acitem" style="display: none;">
</li>
<li class="s">
<li class="s">
Only using class names is not sufficient in your case.
By.cssSelector(".ban") has 15 matching nodes
By.cssSelector(".hot") has 11 matching nodes
By.cssSelector(".ban.hot") has 5 matching nodes
Therefore you need more restrictions to narrow it down. Option 1 and 2 below are available for CSS selector, 1 might be the one that suits your needs best.
Option 1: Using list items' index (CssSelector or XPath)
Limitations
Not stable enough if the site's structure changes
Example:
driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[#id='rightbar']/ul/li[3]/h5"));
Option 2: Using Selenium's FindElements, then index them. (CssSelector or XPath)
Limitations
Not stable enough if a site's structure changes
Not the native selector's way
Example:
// Note that By.CssSelector(".ban.hot") and //*[contains(#class, 'ban hot')] are different, but doesn't matter in your case
IList<IWebElement> hotBanners = driver.FindElements(By.CssSelector(".ban.hot"));
IWebElement banUsStates = hotBanners[3];
Option 3: Using text (XPath only)
Limitations
Not for multilanguage sites
Only for XPath, not for Selenium's CssSelector
Example:
driver.FindElement(By.XPath("//h5[contains(#class, 'ban hot') and text() = 'us states']"));
Option 4: Index the grouped selector (XPath only)
Limitations
Not stable enough if the site's structure changes
Only for XPath, not CssSelector
Example:
driver.FindElement(By.XPath("(//h5[contains(#class, 'ban hot')])[3]"));
Option 5: Find the hidden list items link by href, then traverse back to h5 (XPath only)
Limitations
Only for XPath, not CssSelector
Low performance
Tricky XPath
Example:
driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(#href, 'geo.craigslist.org/iso/us/al')]]/h5"));
By.cssSelector(".ban") or By.cssSelector(".hot") or By.cssSelector(".ban.hot") should all select it unless there is another element that has those classes.
In CSS, .name means find an element that has a class with name. .foo.bar.baz means to find an element that has all of those classes (in the same element).
However, each of those selectors will select only the first element that matches it on the page. If you need something more specific, please post the HTML of the other elements that have those classes.
You can describe your CSS selection like cascading style sheet rows:
protected override void When()
{
SUT.Browser.FindElements(By.CssSelector("#carousel > a.tiny.button"))
}