WebDriver can't select from JQuery dropdown - java

I have a web form having JQuery dropdowns. The particular field holds date of birth. The source for the field is:
<div class="tooltipGroup" style="z-index:19;">
<div class="day">
<div class="jqTransformSelectWrapper" style="z-index: 19;">
<div>
<ul style="width: 100%; display: block; visibility: visible;">
<li class="optHeading">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<li class="undefined">
<a index="6" href="#">6</a>
</li>
<li class="undefined">
<a index="31" href="#">31</a>
</li>
That's the code trying to get all of the elements and put them in a HashMap:
public void selectDob(int dob) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}
dropdownValues.get(dob).click();
}
The code works just fine with one exception: it can't get the values of all fields, just the first visible when the dropdown is being opened.
1 2 3 4 5
The question is how to get the values of the other fields?

I would like to thank you guys for your help (especially HemChe).
It turned out that this is a bug in the latest FirefoxDriver version 2.32. The same code worked just fine with ChromeDriver and I've got all of the drropdown values. Downgrading the selenium version to 2.31 solved that problem and the code works with both drivers!
I'll register a bug on the Selenium bug tracker!
That's the final version of my code:
public void selectFromDropdown(String option) {
WebElement dobFieldDropdown;
WebElement content = driver.findElement(By.className("leftClmn"));
driver.findElement(By.id("aWrapper_dob_day")).click();
dobFieldDropdown = content.findElements(By.className("tooltipGroup")).get(2).findElement(By.className("day")).findElement(By.tagName("ul"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown.findElements(By.tagName("a"))) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText().toString());
}
dropdownValues.get(option).click();
}
Cheers!

Try the below code and check if it is working.
WebElement w = driver.findElement(By.id("aWrapper_dob_day"));
w.click();
WebElement dobFieldDropdown = driver.findElements(By.className("undefined"));
HashMap<String, WebElement> dropdownValues = new HashMap<String, WebElement>();
for (WebElement el : dobFieldDropdown) {
dropdownValues.put(el.getText(), el);
System.out.println(el.getText());
}

You can't locate invisible elements with Web Driver, you need to use JavaScript in order to obtain them. So try something like
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("get them here by class name");

Related

Pagination in selenium

I'm new to selenium and following a small tutorial on scraping that scrapes jobs from indeed.com, but I am having issues as it seems some of the elements have been renamed since the tutorial was written. I'm stuck on this part:
List<WebElement> pagination = driver.findElements(By.xpath("//ul[#class='pagination-
list']/li"));
int pgSize = pagination.size();
for (int j = 1; j < pgSize; j++) {
Thread.sleep(1000);
WebElement pagei = driver.findElement(By.xpath("(//ul[#class='pagination-list']/li)[" + j + "]"));
pagei.click();
The xpath needs to be updated as the element can no longer be found. I have changed the xpath to this, but the list does not get populated:
//nav[#aria-label='pagination']
When I do this, it prints zero which means no elements have been added to the list:
List<WebElement> pagination = driver.findElements(By.xpath("//nav[#aria-label='pagination']"));
int size = pagination.size();
System.out.println(size);
Is this the correct xpath? I'm not certain as to what is supposed to get populated into the list of Webelements? Should they be page numbers?
You could try this path //nav[#role='navigation']/div This would give you 6 elements. 5 for the pages and 1 for the next button
If this is not the correct page, please share the URL and the element, I will try to provide a solution.
The website indeed.com have changed since the article Web Scraping using selenium and Java was written. The pagination elements have changed. Currently the pagination is implemented through:
<nav role="navigation" aria-label="pagination" class="css-jbuxu0 ecydgvn0">
<div class="css-tvvxwd ecydgvn1">
<button data-testid="pagination-page-current" class="css-1cpyzlr e8ju0x51">1</button>
</div>
<div class="css-tvvxwd ecydgvn1">
<a data-testid="pagination-page-2" rel="nofollow" aria-label="2" href="/jobs?q=Api+Testing&l=Pune&start=10" class="css-e9oyys e8ju0x50">2</a>
</div>
<div class="css-tvvxwd ecydgvn1">
<a data-testid="pagination-page-3" rel="nofollow" aria-label="3" href="/jobs?q=Api+Testing&l=Pune&start=20" class="css-e9oyys e8ju0x50">3</a>
</div>
<div class="css-tvvxwd ecydgvn1">
<a data-testid="pagination-page-4" rel="nofollow" aria-label="4" href="/jobs?q=Api+Testing&l=Pune&start=30" class="css-e9oyys e8ju0x50">4</a>
</div>
<div class="css-tvvxwd ecydgvn1">
<a data-testid="pagination-page-5" rel="nofollow" aria-label="5" href="/jobs?q=Api+Testing&l=Pune&start=40" class="css-e9oyys e8ju0x50">5</a>
</div>
<div class="css-tvvxwd ecydgvn1">
<a data-testid="pagination-page-next" rel="nofollow" aria-label="Next Page" href="/jobs?q=Api+Testing&l=Pune&start=10" class="css-cy0uue e8ju0x50">
<svg xmlns="http://www.w3.org/2000/svg" focusable="false" role="img" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true" class="css-1xqhio eac13zx0">
<path d="M9.888 5.998a.506.506 0 00-.716-.008l-.707.707a.506.506 0 00.01.716L13.06 12l-4.587 4.587c-.2.2-.204.521-.009.716l.707.707a.507.507 0 00.717-.009l5.647-5.648c.1-.1.148-.233.144-.366a.492.492 0 00-.144-.34v-.001a.611.611 0 00-.009-.009L9.888 5.998z">
</path>
</svg>
</a>
</div>
</nav>
Solution
Instead of creating a list of the pagination elements, as the pagination keeps on increasing as you move forward you can use the following solution:
System.setProperty("webdriver.chrome.driver", "C:\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://in.indeed.com/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
// Create a Scanner object to prompt for user input
Scanner myObj = new Scanner(System.in);
System.out.println("What jobs are you looking for ? ");
String job = myObj.nextLine();
System.out.println("Which city are you looking for ? ");
String city = myObj.nextLine();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#text-input-what"))).sendKeys(job);
driver.findElement(By.cssSelector("input#text-input-where")).sendKeys(city + Keys.RETURN);
for (int j=1; j<6; j++)
{
driver.findElement(By.xpath("//button[#data-testid='pagination-page-current']//following::div[1]/a[#data-testid]")).click();
Thread.sleep(15000);
}
Note:
Here 6 is the number of (pages-1) you want to scrape.
Thread.sleep(15000) is to provide the window to pulate Email Address after the first click on pagination item.

how to display span class field

i am trying to display two "text text-pass" from html in chrome browser to my print console, apparently, it did not work, any advise please?
my browser html code
<a href="/abc/123" class="active">
<div class="sidebar-text">
<span class="text text-pass"> </span> </a>
<a href="/abc/1234" class="active">
<div class="sidebar-text">
<span class="text text-pass"> </span> </a>
My code
String 123= driver.findElement(By.xpath("//*[#id="js-app"]/div/div/div[2]/div[1]/div/div/ul/li[5]/a")).getText();
System.out.println(123);
String 1234= driver.findElement(By.xpath("//*[#id="js-app"]/div/div/div[2]/div[1]/div/div/ul/li[5]/a")).getText();
System.out.println(1234);
You can use .findElements to get multiple elements with the same pattern, it will return a list collection.
UPDATE
Refers to your comment, you need put the string into a list again and check with the Collection.contains() method:
List<String> results = new ArrayList<>();
List<WebElement> elements = driver.findElements(By.xpath("//div[#class='sidebar-text']//span"));
for(WebElement element: elements) {
String attr = element.getAttribute("class");
results.add(attr);
System.out.println(attr);
}
if(results.contains("text text-fail")) {
System.out.println("this is list contains 'text text-fail'");
}
Try this Code :
String pass = driver.findElement(By.xpath("//*[#class='sidebar-text']/span")).getAttribute("class");
System.out.println(pass);

Need help- Selenium webdriver- click on element from visible list is not working

I am sending defined data from excel file. I tried some code but they are not selecting all the data from excel file at some point of time code is giving me exception for WebElement not found.
Here is the HTML code:
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
<div id="addDialog" class="hidden ui-dialog-content ui-widget-content" style="display: block; width: auto; min-height: 30px; height: auto; max-height: 351.05px; overflow-y: auto;">
<div class="field-container">
<fieldset class="field-container">
<legend>Contracts:</legend>
<a class="select-all" href="#">Select All</a>
<a class="deselect-all" href="#">Deselect All</a>
<select id="addContract" class="searchable" multiple="multiple" style="position: absolute; left: -9999px;">
<option value="93370956">93370956</option>
<option value="93796167">93796167</option>
<option value="94203239">94203239</option>
</select>
<div id="ms-addContract" class="ms-container">
<div class="ms-selectable">
<input class="search-input" type="text" placeholder="filter" autocomplete="off"/>
<ul class="ms-list" tabindex="-1">
<li id="86355560-selectable" class="ms-elem-selectable">
<span>93370956</span>
</li>
<li id="202890296-selectable" class="ms-elem-selectable">
<span>93796167</span>
</li>
<li id="938848030-selectable" class="ms-elem-selectable">
<span>94203239</span>
</li>
</ul>
</div>
</div>
Need to select values from list.
Efforts done:
This effort for code worked but it selected only one value and then gave exception
WebDriverWait Wait=new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul[#class='ms-list']/li/span")));
//now you can find element
List<WebElement>options=driver.findElements(By.xpath("//ul[#class='ms-list']/li/span[contains(text(),'"+testData+"')]"));
for (WebElement option: options) {
if(testData.equals(option.getText())) option.click();
}
Tried above code but it only selects one value !!
WebDriverWait Wait = new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='ms-addContract']//descendant::div[#class='ms-selectable']/ul[#class='ms-list']]//span")));
List<WebElement> options = driver.findElements(By.xpath("//*[#id='ms-addContract']//descendant::div[#class='ms-selectable']/ul[#class='ms-list']]//span[contains(text(), '"+testData+"')]"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.click();--tried this xpath-no success
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='ms-addContract']/div[1]/input"))).click();
driver.findElement(By.xpath(".//*[#id='ms-addContract']/div[1]/input")).sendKeys(testData);
WebDriverWait wait1 = new WebDriverWait(driver, 10);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='ms-addContract']/div[1]/input"))).sendKeys(Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.SPACE);
//Clear the input text value.
driver.findElement(By.xpath(".//*[#id='ms-addContract']/div[1]/input")).clear();---worked but not satisfactory
Please need help to locate the element. I am using keyword driven framework reading values from excel file.
It sounds like you are trying to get all span elements that are contained within a ul element (although correct me if I am wrong). This is the code you are using:
List<WebElement>options = driver.findElements(By.xpath("//ul[#class='ms-list']/li/span[contains(text(),'"+testData+"')]"));
In your case, this XPath will only return 1 value. This is because you are trying to match the contents of each span with an input value "testData". If you want to get all span elements inside of the ul then use:
List<WebElement>options = driver.findElements(By.xpath("//ul[#class='ms-list']/li/span"));
This XPath will select accomplish selecting all span elements in the list. If you need to select only span elements containing text within your testData, then you can iterate over that list selecting the appropriate elements:
ArrayList<WebElement> optionsInTestData = new ArrayList<WebElement>();
for(WebElement element: options){
for(String data: testData){
if(element.getText() == data){
optionsInTestData.add(element);
}
}
}

How to fetch dropdown value from List in selenium Webdriver by using java

How to consider list value as a dropdown value?
How to select Edit Drop down value from List...
<div class="nice-select demoBasic" tabindex="0">
<span class="current">Please select</span>
<ul class="list">
<li class="option" data-value="1">Edit</li>
<li class="option" data-value="2">Delete</li>
</ul>
</div>
I got it solution ...
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div")).click();
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
Thread.sleep(4000);
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div/ul/li[1]")).click();
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
Thread.sleep(4000);
You should try using By.cssSelector() to get all options in list as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//First click on select div
WebElement select = wait.until(ExepectedConditions.elementToBeClickable(By.cssSelector("div.nice-select.demoBasic")));
select.click();
//Not get all nested options
List<WebElement> options = wait.until(ExepectedConditions.presenceOfNestedElementsLocatedBy(select, By.cssSelector("ul.list li.option")));
//Now iterate to fetch all options and select edit option
for(WebElement option : options)
{
if(option.getText().equals("Edit"))
{
option.click();
}
}
Or you can select an option on the basis of text in one line using By.xpath() as below :-
wait.until(ExepectedConditions.elementToBeClickable(By.xpath(".//li[text() = 'Edit']"))).click();

Automating Combo Box (drop down + checkbox) using Selenium

I am trying to automate the drop down in the website Naukri.com. That drop down consists of multi select check-boxes. How can we automate it using Selenium Web driver?
The structure of the drop list is:
<div class="DDwrap">
<ul class="DDsearch">
<li class="tagit" data-id="tg_indCja_a8_A">
<span class="tagTxt">Accounting , Finance</span>
<span class="dCross"></span>
</li>
<li class="frst" style="float: left;">
<input id="cjaInd" class="srchTxt" type="text" placeholder="" name="" autocomplete="off" style="width: 30px;">
<input id="hid_indCja" type="hidden" name="indType" value="["8"]">
</li>
</ul>
</div>
Can anyone help me regarding this?
Check out the code below, It navigates to the concerned form, opens the dropdown of "Industry" and selects two checkboxes: 'Accounting , Finance' and 'Government , Defence':
WebDriver driver = new FirefoxDriver(); //Opening firefox instance
driver.manage().window().maximize(); //maximizing window
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Giving implicit timeout of 20 seconds
driver.get("http://www.naukri.com/");
//Since there are two windows popping up, hence switching and closing the unnecessary window.
Set<String> windows = driver.getWindowHandles();
Iterator iter = windows.iterator();
String parentWindow = iter.next().toString();
String childWindow = iter.next().toString();
driver.switchTo().window(childWindow);
driver.close();
driver.switchTo().window(parentWindow);
//Hovering over "Jobs"
Actions act = new Actions(driver);
WebElement jobs = driver.findElement(By.xpath("//ul[#class='midSec menu']//div[.='Jobs']"));
act.moveToElement(jobs).build().perform();
//Clicking on "Advance Search"
WebElement Adv_search = driver.findElement(By.xpath("//ul[#class='midSec menu']/li[1]//a[.='Advanced Search']"));
act.moveToElement(Adv_search).click().perform();
//Clicking on the industry dropdown
driver.findElement(By.xpath("//div[#class='DDinputWrap']/input[contains(#placeholder,'Select the industry')]")).click();
//Selecting the checkbox containing text as "Accounting"
driver.findElement(By.xpath("//ul[#class='ChkboxEnb']//a[contains(text(),'Accounting')]")).click();
//Selecting the checkbox containing text as 'Government'
driver.findElement(By.xpath("//ul[#class='ChkboxEnb']//a[contains(text(),'Government')]")).click();

Categories