2016-07-29 50 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()方法來執行。

希望它能幫助.. :)