Getting list of items inside div using Selenium Webdriver - java

Say I have the following
<div class="facetContainerDiv">
<div>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
</div>
</div>
Now I want to put a check mark on the checkbox based on the index I provide. So I write a method like below
How do I access all elements inside the div class="facetContainerDiv" ?
I tried
List<WebElements> elementsList = driver.findElements(By.cssSelector(".facetContainerDiv"));
for(WebElement checkBox:elementsList) {
int i=0;
checkBox = elementsList.get(i);
bla bla bla..
}
in the above code elementsList has only one element with "type" as null.

Follow the code below exactly matched with your case.
Create an interface of the web element for the div under div with class as facetContainerDiv
ie for
<div class="facetContainerDiv">
<div>
</div>
</div>
2. Create an IList with all the elements inside the second div i.e for,
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
<label class="facetLabel">
<input class="facetCheck" type="checkbox" />
</label>
3. Access each check boxes using the index
Please find the code below
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTests
{
class ChechBoxClickWthIndex
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("file:///C:/Users/chery/Desktop/CheckBox.html");
// Create an interface WebElement of the div under div with **class as facetContainerDiv**
IWebElement WebElement = driver.FindElement(By.XPath("//div[#class='facetContainerDiv']/div"));
// Create an IList and intialize it with all the elements of div under div with **class as facetContainerDiv**
IList<IWebElement> AllCheckBoxes = WebElement.FindElements(By.XPath("//label/input"));
int RowCount = AllCheckBoxes.Count;
for (int i = 0; i < RowCount; i++)
{
// Check the check boxes based on index
AllCheckBoxes[i].Click();
}
Console.WriteLine(RowCount);
Console.ReadLine();
}
}
}

I'm not sure if your findElements statement gets you all the divs. I would try the following:
List<WebElement> elementsRoot = driver.findElements(By.xpath("//div[#class=\"facetContainerDiv\"]/div));
for(int i = 0; i < elementsRoot.size(); ++i) {
WebElement checkbox = elementsRoot.get(i).findElement(By.xpath("./label/input"));
checkbox.click();
blah blah blah
}
The idea here is that you get the root element then use another a 'sub' xpath or any selector you like to find the node element. Of course the xpath or selector may need to be adjusted to properly find the element you want.

You're asking for all the elements of class facetContainerDiv, of which there is only one (your outer-most div). Why not do
List<WebElement> checks = driver.findElements(By.class("facetCheck"));
// click the 3rd checkbox
checks.get(2).click();

alternatively, you can try writing a specific element:
//label[1] is the first element.
el = await driver.findElement(By.xpath("//div[#class=\"facetContainerDiv\"]/div/label[1]/input")));
await el.click();
More information can be found here: https://www.browserstack.com/guide/locators-in-selenium

Related

JSoup - Get tag based on text

Suppose I have 3 text boxes on a page defined as below.
<input id="input" type="search" autocomplete="off" role="combobox" placeholder="Search">
<input id="input" type="open" autocomplete="off" role="combobox" placeholder="Open">
<input id="input" type="close" autocomplete="off" role="combobox" placeholder="Close">
I will pass the value 'Open' as a parameter to JSoup and JSoup should return me the data as below (which are the details of the middle textbox).
<input id="input" type="open" autocomplete="off" role="combobox" placeholder="Open">
Can JSoup do this?
Thank You
-Anoop
You need to select tag by attribute:
document.select("input[placeholder=Open]")
UPD:
To select tag has any attribute with value "Open" you need to iterate over all attribute values:
List<Element> result = document.select("input").stream()
.filter(input -> hasAttrValue(input, "Open"))
.collect(Collectors.toList());
hasAttrValue method:
private boolean hasAttrValue(Element element, String targetValue) {
for (Attribute attribute : element.attributes()) {
if (targetValue.equals(attribute.getValue())) {
return true;
}
}
return false;
}

Finding previous sibling element using selenium xpath dynamically

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]"));

getting NullPointer Exception in Handling AutoSuggestion in Selenium Java

I am Automating add to card process of "http://www.fnp.com/the-sweet-surprises-genpr-143431-e.html" website. I am writing a code Using TestNG POM Structure . I got Stuck in task where I am getting NullPointer Exception in Handling Autosuggestion. Below is my POM Class
public class productPage_POM {
public WebDriver driver;
#FindBy(id="tagsf2")
private WebElement city_txtbox;
public productPage_POM(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
public void sel_adr()
{
city_txtbox.sendKeys("gan"); // on this line i am gettign nullpointer Exception
String xp = "//a[starts-with(text(),'gan')]";
List<WebElement> allList = driver.findElements(By.xpath(xp));
int count = allList.size();
System.out.println(count);
for (int i = 0; i < count ; i++)
{
String name = allList.get(i).getText();
System.out.println(name);
if(name.contains("Gandhinagar"))
{
allList.get(i).click();
break;
}
}
Below is HTML structure
<div class="pro_detail">
<div id="changeImage" class="pro_detail_image">
<div class="pro_detail_title">
<div class="pro_detail_size">
<div class="pro_detail_form">
<input id="cityIdHidden" type="hidden" value="CITY-1219">
<input id="hasShippingOption" type="hidden" value="">
<input id="cityIdHidden1" type="hidden" value="">
<input id="liCount" type="hidden" value="1">
<div id="hiddenField">
<div class="pro_detail_form_inn">
<label>1. Where do you want to deliver this?</label>
<input id="tagsf2" class="inp_pro_detail2 election ui-autocomplete-input" type="text" value="Enter City" onchange="showAddtocart();" onblur="WatermarkOnblur('Enter City','tagsf2');shippingOption();" onfocus="WatermarkOnfocus('Enter City','tagsf2')" autocomplete="off">
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">CITY-1219</span>
<div class="clear"></div>
<div id="cityNotAvailable" class="error_message hide" style="display: none;">
</div>
<div class="textbox">
</div>
<div class="pro_detail_form">
<div id="divDeliveryText" class="pro_detail_form" style="display:none;">
<div id="valentineCatalougueText" class="pro_detail_form" style="display:block;">
<div class="pro_detail_form">
The below code will work:
String xp = "//ul/li/a[#class='ui-corner-all']";
List<WebElement> allList = driver.findElements(By.xpath(xp));
int count = allList.size();
System.out.println(count);
for (int i = 0; i < count ; i++)
{
String name = allList.get(i).getText();
System.out.println(name);
if(name.contains("Gandhinagar"))
{
allList.get(i).click();
break;
}
}
Initialize your WebElement before click or sendkeys.
WebElement webelement = getDriver().findElement(By.id("tagsf2"));
webelement.sendKeys("gan");
Remaining logic seems correct. Just make sure your suggetion field's xpath is correct.
I think autocomplete creates a list. So it should be
String xp = "//li[starts-with(text(),'gan')]";
WebDriverWait wait = new WebDriverWait(driver,40 );
wait.until(ExpectedConditions.presenceOfElementsLocated(By.xpath(xp)));
I think it takes time for the autocomplete to load, but there should be some delay after which you should try to find the elements. Try WebDriverWait and let me know.

Thymeleaf: Iteration - increment by N and access list.get(N)

So I've read the Iterations part of the documentation already but still didn't give any idea on how to do the following:
Iterate per 2 records (since I am rendering something like below) and access list by index.
<div class="row">
<div class="col-md-6">
...
</div>
<div class="col-md-6">
...
</div>
</div>
Basically if this were in code, it looks something like
for (int i = 0; i < size;) {
// do stuff
// manual increment
if (i + 2 > size) {
i++;
} else {
i += 2;
}
}
Any other approach that would satisfy my problem is always welcome too!
I was able to solve it using something like below:
Basically, I still loop individually but just skip every other record using th:if="${stat.even}" and just get the next record by stat.index + 1.
Be really cautious about the IndexOutOfBoundsException though.
<div class="row" th:each="hivRisk, stat : ${hivRiskList}" th:if="${stat.even}">
<div class="col-md-6" th:with="leftRisk=${hivRiskList.get(stat.index)}">
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" value="-1" th:value="${leftRisk.id}"/>
<span th:text="${leftRisk.name}">HIV Risk</span>
</label>
</div>
</div>
<div class="col-md-6" th:if="${stat.index + 1 < hivRiskList.size()}" th:with="rightRisk=${hivRiskList.get(stat.index + 1)}">
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" value="-1" th:value="${rightRisk.id}"/>
<span th:text="${rightRisk.name}">HIV Risk</span>
</label>
</div>
</div>
</div>

Conditional Statements II

Those are the fields that I have that I want to write a conditional statement for in a PDF form that I am creating in Adobe Acrobat Pro X. In the form if I tick the checkbox I would like FP1 to get the value from QxHxW1. If the checkbox is not ticked I want FP1 to register as "0". I have been trying to do this with different tutorials that I have found online and each time I get some sort of SyntaxError.
is there anything I can do to fix this? Am I way off with the way that this is written?
FrenchPane1 is a checkbox
FP1 is a text box
QxHxW1 is a text box
Using javascript, this would be:
var check = document.getElementsByClassName('check');
for( var i = 0; i < check.length; i++ ){
check[i].onchange = function() {
var isChecked = this.checked;
var target = document.querySelector(this.dataset.target);
var source = document.querySelector(this.dataset.source);
target.value = isChecked ? source.value : '0';
}
}
<div>
<label for="FrenchPane1"><input type="checkbox" id="FrenchPane1" data-target="#FP1" data-source="#QxHxW1" class="check"> FrenchPane1</label>
<input type="text" name="FP1" id="FP1">
<input type="text" name="QxHxW1" id="QxHxW1" value="Some values">
</div>
<div>
<label for="FrenchPane1"><input type="checkbox" id="FrenchPane2" data-target="#FP2" data-source="#QxHxW2" class="check"> FrenchPane1</label>
<input type="text" name="FP2" id="FP2">
<input type="text" name="QxHxW2" id="QxHxW2" value="Some values 2">
</div>

Categories

Resources