How to identify webelement buttons by selenium webdriver
executeScript method is undefined. Where to add this
driver.executeScript("return $('body /deep/ <#selector>')")
?
Asked
Active
Viewed 247 times
0

Mr. Blond
- 1,113
- 2
- 18
- 41

arpita soni
- 1
- 2
-
Try this`((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);`. You will have to change the locators accordingly in your automation code. – Harish Jul 29 '16 at 09:09
-
ya i tried this. It works for button , but for dropdown how to use this element. I want to print dropdown list in console WebElement dropDown = driver.findElement(By.id("countTd")); dropDown.click(); driver.findElement(By.xpath("//td[@id='countTd']/span[text()='"']")).click.getOptions(); – arpita soni Jul 29 '16 at 09:21
-
See this http://stackoverflow.com/questions/6430462/how-to-select-get-drop-down-option-in-selenium-2 – Siva Jul 29 '16 at 09:34
-
@arpitasoni You don't have to use JavascriptExecutor for this. Also, you don't have to click the element as well. Try this code `Select selectElement = new Select(driver.findElement(By.Id("countTd"))); List
options = selectElement.getOptions();`. You can iterate through the options list and print the values using for loop. – Harish Jul 29 '16 at 11:35
3 Answers
0
Try below code for retrieving all dropdown values
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement selectMonth = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@title = 'Birthday']")));
selectMonth.click();
List<WebElement> allmonths = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("span#BirthMonth > div.goog-menu.goog-menu-vertical")));
for(WebElement month : allmonths) {
System.out.println(month.getText());
Hope it will help

Ravi Potnuru
- 191
- 4
- 13
0
We will get this type of Exception in below scenario.
- If pages are not embedded in
jQuery
. jQuery
library is not loaded successfully.- Browser syn isssue.
First check pages is embedded in jQuery
or not by executing below command on browser console
window.jQuery=='undefine' // Its for checking jQuery is present on page if yes then return true.
and
jQuery.active==0 // Its for checking jquery is activated on page if yes then return true.
then try below code
String getArgument="0"; // take single element
//String getArgument="";// take list of matched element
((JavascriptExecutor) driver).executeScript("return $( #selector).get(" + getArgument + ");");

Saurabh Gaur
- 23,507
- 10
- 54
- 73

Eknath
- 624
- 4
- 11
0
You can simply identify element by using getTagName()
as below :-
WebElement element = driver.findElement(By.id("countTd"));
// To verify if element is button
if(element.getTagName().equals("button")) {
element.click();
}
// To verify if element is dropdown
if(element.getTagName().equals("select")) {
// Now pass it to Select class to work
Select selectElement = new Select(element);
// Now you can get all options
List<WebElement> options = selectElement.getOptions();
//Now you can print all options text
for(WebElement option : options) {
System.out.println(option.getText());
}
}
Node :- There is no need to use JavascriptExecutor
to perform click, it would be simply perform by calling .click()
method.
Hope it helps..:)

Saurabh Gaur
- 23,507
- 10
- 54
- 73