I am writing selenium scripts for the following code.
<div id="abc" class="ui-selectmanycheckbox ui-widget hpsapf-chechkbox">
<div class="ui-chkbox ui-widget">
<div class="ui-helper-hidden-accessible">
<input id="abc:0" name="abc" type="checkbox" value="0" checked="checked">
</div>
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active">
<span class="ui-chkbox-icon ui-icon ui-icon-check ui-c"></span>
</div>
</div>
<span class="hpsapf-radio-label">
<label for="abc:0">Herr</label>
</span>
<div class="ui-chkbox ui-widget">
<div class="ui-helper-hidden-accessible">
<input id="abc:1" name="abc" type="checkbox" value="1">
</div>
<div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default">
<span class="ui-chkbox-icon ui-icon ui-icon-blank ui-c"></span>
</div>
</div>
<span class="hpsapf-radio-label">
<label for="abc:1">Frau</label>
</span>
</div>
These are the checkbox like the following.The number of the checkboxes are changed as per database values.
In my code i am first checking whether the "Frau" check box is selected or not. so i tried following snippet.
WebElement mainElement= driver.findElement(By.id("abc"));
WebElement label=mainElement.findElement(By.xpath(".//label[contains(#for,'abc')][text() = 'Frau']"));
WebElement parent = label.findElement(By.xpath(".."));
WebElement div = parent.findElement(By.xpath("preceding-sibling::::div"));
WebElement checkBox = div.findElement(By.className("ui-chkbox-box"));
String css = checkBox.getAttribute("class");
if(css.contains("ui-state-active")) {
return "checked";
}
else
{
return "unchecked";
}
But when i tried to execute this script. WebElement div = parent.findElement(By.xpath("preceding-sibling::::div")); gives me the first div tag and not the preceding one. I want a preceding sibling.
Use :: and index, not ::::
WebElement div = parent.findElement(By.xpath("preceding-sibling::div[1]"));
Related
I'm having problems getting the text contents of an input field. I seem to only be getting the things around it with the method I'm using.
Snippet from the page:
(It's a list of itemsincluding an input field in each row.)
The markup:
<ul class="budsjett budsjett--kompakt" id="sifobudsjett">
<li class="budsjett-post ng-isolate-scope ng-valid" id="SIFO_mat">
<div class="felt" >
<div class="felt-indre">
<div id="SIFO_mat-farge" class="sifo-farge farge-graa"></div>
<span class="budsjett-post-beskrivelse" >
<span tabindex="0" title="Vis hjelpetekst" role="button">
<span class="hjelpetekst-label" >Mat og drikke</span>
</span>
<span class="sifo-hjelp" aria-hidden="true"></span>
</span>
</span>
<span class="budsjett-post-verdi">
<span class="budsjett-post-verdi-endret" ng-show="!skrivebeskyttet" aria-hidden="false" style="">
<input id="SIFO_mat-input" name="SIFO_mat" type="number">
<span class="felt-enhet"><abbr id="SIFO_mat-enhet" title="kroner" translate=""><span class="ng-scope">kr</span></abbr></span>
</span>
</span>
</div>
</div>
</li>
The code:
List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.tagName("li"));
Result of first element: "Mat og drikke".
List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.tagName("input"));
Result of first element: ""
List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.className("budsjett-post-verdi-endret"));
Result of first element: "kr"
Any ideas?
The <input> tag doesn't have text, what you see in the UI is kept in the value attribute. It exists even if you can't see it in the html
driver.findElement(By.id("SIFO_mat-input")).getAttribute("value");
For all the <input>s
List<WebElement> sifoliste = driver.findElement(By.id("sifobudsjett")).findElements(By.tagName("input"));
String text = sifoliste.get(0).getAttribute("value"); // 2790
Try
String inputValue = driver.findElement(By.tagName("input")).getAttribute("value");
I have the following element:
<table class="dijit " data-dojo-attach-point="_buttonNode" cellspacing="0" cellpadding="0" role="lbox" aria-haspopup="true" tabindex="0" id="POS_domain" data-id="domain" widgetid="POS_domain" aria-expanded="false" aria- invalid="false" style="user-select: none;" popupactive="true" aria-owns="POS_domain">
<tbody role="presentation">
<tr role="presentation">
<td class="dijitReset" role="presentation">
<div class="dijitReset Text" data-dojo-attach-point="container" role="presentation">
<span role="option" aria-selected="true" class="dijitLabel ">adrija</span>
</div>
<div class="dijitContainer">
<input class="dijitInner" value="Χ " type="text" tabindex="-1" readonly="readonly" role="presentation">
</div>
<input type="hidden" data-dojo-attach-point="vn" value="adrija" hidden="true">
</td>
<td class="dijitArrowButtonContainer" data-dojo-attach-point="titleNode" role="presentation">
<input class="dijitInner" value="▼ " type="text" tabindex="-1" readonly="readonly" role="presentation">
</td>
</tr>
</tbody>
</table>
The above element is an element of dropdown and is hidden. The code that I have written is:
private WebElement domainDropdown = Driver.driver.findElement(By.id("POS_domain"));
domainDropdpwn.click();
private WebElement adrija = Driver.driver.findElement(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", adrija);
It says it's not able to find the element.
Please help. Thanks. :)
The desired <input> tag is having the attributes type="hidden" and hidden="true", so to click() on the element you can use the following solution:
//driver being an instance of WebDriver
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[#class='dijit ' and #id='POS_domain']"))).click();
WebElement my_adrija = driver.findElement(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('hidden')", my_adrija)
((JavascriptExecutor)driver).executeScript("arguments[0].setAttribute('type','text')", my_adrija)
WebElement my_new_adrija = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", my_new_adrija);
what i have tried:
Select listbox = new Select(
driver.findElement(By.xpath("//*[#id='multiselect_categories']"))
);
listbox.selectByValue("ATM");
Html code when some option choose:
<input name="multiselect_categories" id="multiselect_categories"
type="text" autocomplete="off" placeholder="Select option"
tabindex="0" class="multiselect__input" style="display: none;">
<div class="multiselect__tags">
<div class="multiselect__tags-wrap" style="">
<span class="multiselect__tag">
<span>Actions and Practices</span>
<i aria-hidden="true" tabindex="1" class="multiselect__tag-icon"></i>
</span>
<span class="multiselect__tag">
<span>Air Carrier Services and Safety Oversight</span>
<i aria-hidden="true" tabindex="1" class="multiselect__tag-icon"></i>
</span>
</div>
<div class="multiselect__spinner" style="display: none;"></div>
<input name="multiselect_categories" id="multiselect_categories"
type="text" autocomplete="off" placeholder="Select option"
tabindex="0" class="multiselect__input"
style="width: 0px; position: absolute; padding: 0px; display: none;">
</div>
<div class="multiselect__content-wrapper" style="max-height: 291.375px; display: none;">
<ul class="multiselect__content" style="display: inline-block;">
<li class="multiselect__element">
<span data-select="Press enter to select" data-selected="Selected"
data-deselect="Press enter to remove" class="multiselect__option">
<span>ATM</span>
</span>
</li>
<li class="multiselect__element">
<span data-select="Press enter to select" data-selected="Selected"
data-deselect="Press enter to remove" class="multiselect__option
multiselect__option--selected">
<span>Actions and Practices</span>
</span>
</li>
<li class="multiselect__element">
<span data-select="Press enter to select" data-selected="Selected"
data-deselect="Press enter to remove" class="multiselect__option
multiselect__option--selected">
<span>Air Carrier Services and Safety Oversight</span>
</span>
</li>
</ul>
</div>
CODE that failed when adding to selenium code when adding to testng:
#Test(description = "Test5")
public void chooseCatagory(String... catagories) {
for(String catagory: catagories) {
// input catagory in text box which display placeholder `Select option`
driver.findElement(By.cssSelector("div.multiselect__tags #multiselect_categories"))
.sendKeys(catagory);
// find the item from auto-suggest list
driver.findElement(By.cssSelector("div.multiselect__tags + div > ul"))
.findElement(By.xpath("./li//span[text()='"+catagory+"']"))
.click();
}
}
chooseCatagory("ATM", "Airports");
Error from the above code:
org.testng.TestNGException:
Cannot inject #Test annotated Method [chooseCatagory] with [class [Ljava.lang.String;].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection
org.testng.TestNGException:
HTML when there is nothing chosen:
<input name="multiselect_categories" id="multiselect_categories"
type="text" autocomplete="off" placeholder="Select option" tabindex="0" class="multiselect__input" style="display: none;">
<span><span class="multiselect__single">
Select option
</span></span>
what the list contains:
ATM,Action, refer to screenshot
#Test(description = "Test5")
public test_chooseCatagory() {
chooseCatagory("ATM", "Airports");
}
private void chooseCatagory(String... catagories) {
for(String catagory: catagories) {
// click the down arrow at right to make the filter text box and
// all option list display
driver.findElement(By.cssSelector("div.multiselect__select"))
.click();
// input catagory into text box to filter matched options
driver.findElement(By.cssSelector(".multiselect__tags #multiselect_categories"))
.sendKeys(catagory);
// click the option from filtered option list
driver.findElement(By.cssSelector(".multiselect__content-wrapper > ul"))
.findElement(By.xpath("./li//span[text()='"+catagory+"']"))
.click();
// sleep 2 seconds before next choosing
try {
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
So i have the following HTML Code of a listbox here:
<div role="listbox" aria-expanded="false" class="quantumWizMenuPaperselectEl docssharedWizSelectPaperselectRoot freebirdFormviewerViewItemsSelectSelect freebirdThemedSelectDarkerDisabled" jscontroller="YwHGTd" jsaction="click:cOuCgd(LgbsSe); keydown:I481le; keypress:Kr2w4b; mousedown:UX7yZ(LgbsSe),npT2md(preventDefault=true); mouseup:lbsD7e(LgbsSe); mouseleave:JywGue; touchstart:p6p2H(LgbsSe); touchmove:FwuNnf; touchend:yfqBxc(LgbsSe|preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd(LgbsSe); focus:AHmuwe; blur:O22p3e;b5SvAb:TvD9Pc;" jsshadow="" jsname="W85ice" aria-describedby="i.desc.709120473 i.err.709120473" aria-labelledby="i73">
<div jsname="LgbsSe" role="presentation">
<div class="quantumWizMenuPaperselectOptionList" jsname="d9BH4c" role="presentation">
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption isSelected isPlaceholder" jsname="wQNmvb" jsaction="" data-value="" aria-selected="true" role="option" tabindex="0">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">Auswählen</content>
</div>
<div class="quantumWizMenuPaperselectOptionSeparator" role="presentation"></div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="140 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">140 cm</content>
</div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="141 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">141 cm</content>
</div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="142 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">142 cm</content>
</div>
<div class="quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption" jsname="wQNmvb" jsaction="" data-value="143 cm" aria-selected="false" role="option" tabindex="-1">
<div class="quantumWizMenuPaperselectRipple exportInk" jsname="ksKsZd"></div>
<content class="quantumWizMenuPaperselectContent exportContent">143 cm</content>
</div>
</div>
<div class="quantumWizMenuPaperselectDropDown exportDropDown" role="presentation"></div>
</div>
<div class="exportSelectPopup quantumWizMenuPaperselectPopup" jsaction="click:dPTK6c(wQNmvb); mousedown:uYU8jb(wQNmvb); mouseup:LVEdXd(wQNmvb); mouseover:nfXz1e(wQNmvb); touchstart:Rh2fre(wQNmvb); touchmove:hvFWtf(wQNmvb); touchend:MkF9r(wQNmvb|preventMouseEvents=true)" role="presentation" jsname="V68bde" style="display:none;"></div>
</div>
I am writing an program which has to select an element of this listbox automatically in java (like "140 cm", "141 cm" like you see in the code etc...). I tried to access the listbox itself with the following code:
WebElement checkBox = driver.findElement(By.cssSelector("div[aria-labelledby*=i73]"));
CheckBox.click();
It worked but now i have to select somehow an element of this listbox. I tried it with the 'Select'-Command, which did not work:
Select listbox = new Select(checkBox);
listbox.selectByVisibleText("140 cm");
I also tried it with clicking on the specific div with the '140 cm' text and waiting for its clickability. But I get a timeout exception because it failed to wait for the element to be clickable.
WebElement boxElement = driver.findElement(By.cssSelector("div[data-value*='140']"));
WebDriverWait wait = new WebDriverWait(driver, 10);
boxElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[data-value*='140']")));
boxElement.click();
I am desperate and do not know what to do. Can any of you guys help me? I am thankfully for every answer!
greetings
I'm new to Selenium webdriver. Can someone help me how to get the span element in table row
<span class="small-info" title="zim.lu#en.com , stins.gib#en.com "> zim.lu#en.com , stin.gib#en.com </span>
in below Table
<table class="k-selectable" role="grid" data-role="selectable">
<colgroup>
<tbody role="rowgroup">
<tr class="k-state-selected" role="row" data-uid="39c56242-2108-4b6d-b80f-1e2f266cd02f" aria-selected="true">
<td role="gridcell">
<div class="left-info">
<div id="item193689" class="inbox-info">
<div class="left-inboxInfo">
<h2 class="SubjecthOverflow">
<span class="small-info" title="zim.lu#en.com , stins.gib#en.com "> zim.lu#en.com , stin.gib#en.com </span>
<div id="policydiv193689">
</div>
<div class="right-inboxInfo">
</div>
</td>
</tr>
<tr class="k-alt" role="row" data-uid="32a122c7-2e7b-4a28-bb77-5fde6679e6ec">
<td role="gridcell">
<div class="left-info">
<div id="item202147" class="inbox-info">
<div class="left-inboxInfo">
<h2 class="SubjecthOverflow">
<span class="small-info" title="kev.kind#en.com , vin.kami#en.com "> ke.kin#en.com , vi.kami#en.com </span>
<div id="policydiv202147">
</div>
<div class="right-inboxInfo">
</div>
</td>
</tr>
</tbody>
</table>
I tried this code
WebElement table_element = dr.findElement(By.className("k-selectable"));
List<WebElement>tr_collection=table_element.findElements(By.xpath("//span[#class='small-info']"));
System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
Output not showing
List<WebElement>tr_collection=dr.findElements(By.xpath("//span[#class='small-info']"));
System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
You can find the element by using xpath or css selector
Try this below code in your list
dr.findElements(By.xpath("//*[#role='grid']/colgroup/tbody/tr/td/div/div/div/h2/span"));