2016-07-29 51 views
0

如何通过硒webdriver 识别webelement按钮未定义executeScript方法。在哪里添加这个 driver.executeScript("return $('body /deep/ <#selector>')")如何使用硒webdriver识别webelements(按钮,下拉等)

+0

尝试this'((JavascriptExecutor)驱动程序).executeScript( “参数[0]。点击();”,元素);'。您必须在自动化代码中相应地更改定位器。 – Harish

+0

你试过这个。它适用于按钮,但下拉如何使用此元素。我想在控制台中打印下拉列表WebElement dropDown = driver.findElement(By.id(“countTd”)); dropDown.click(); driver.findElement(Byxpath(“// td [@ id ='countTd']/span [text()=''']”))。click.getOptions(); –

+0

看到这个http:// stackoverflow.com/questions/6430462/how-to-select-get-drop-down-option-in-selenium-2 – Siva

回答

0

试试下面的代码检索所有下拉值

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()); 

希望这将有助于

0

,我们会得到下面的情形这种类型的异常。

  1. 如果页面未嵌入jQuery
  2. jQuery库未成功加载。
  3. 浏览器同步。

首先检查网页被嵌入jQuery或不通过浏览器控制台下面的命令执行

window.jQuery=='undefine' // Its for checking jQuery is present on page if yes then return true. 

jQuery.active==0 // Its for checking jquery is activated on page if yes then return true. 

然后尝试

String getArgument="0"; // take single element 
//String getArgument="";// take list of matched element 

((JavascriptExecutor) driver).executeScript("return $(#selector).get(" + getArgument + ");"); 
0

您可以在下面的代码只需使用标识元素即可getTagName()如下: -

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()); 
    } 
} 

节点: - 有没有必要使用JavascriptExecutor进行点击,就可以简单地通过调用.click()方法来执行。

希望它能帮助.. :)