2017-05-05 38 views
0

任何人都可以幫我解決我的問題嗎?在下面的代碼中,click()方法在下拉列表中不起作用。方法click()在下拉列表中不起作用

package com.driver; 

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Listdown { 

    static WebDriver driver; 

    public static void main(String[] args) { 
     driver = new FirefoxDriver(); 
     driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
     driver.manage().window().maximize(); 

     driver.get("https://accounts.google.com/SignUp?service=mail&hl=ru&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fpc%3Dtopnav-about-ru"); 

     driver.findElement(By.xpath("(//strong[text()='Пол']/following-sibling::div/div[@role='listbox'])[1]")).click(); 
     driver.findElement(By.xpath("//div[text()='Мужской']/parent::div[@role='option']")).click(); 
    } 
} 

在結果我必須收到標記複選框「Мужской」,但我沒有。

添加HTML代碼:

<div id="gender-form-element" class="form-element"> 
    <label id="gender-label"> 
    <strong id="GenderLabel">Пол</strong> 
    <div id="Gender" class=" form-error" aria-invalid="true"> 
     <div class="goog-inline-block goog-flat-menu-button jfk-select" role="listbox" style="-moz-user-select: none;" aria-expanded="false" tabindex="0" aria-haspopup="true" title="Пол" aria-activedescendant=":d"> 
     <div class="goog-menu goog-menu-vertical" style="-moz-user-select: none; visibility: visible; left: 0px; top: 21.5px; display: none;" role="listbox" aria-haspopup="true"> 
      <div id=":e" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
      <div id=":f" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
       <div class="goog-menuitem-content">Мужской</div> 
      </div> 
      <div id=":g" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
      <div id=":h" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     </div> 
     <input id="HiddenGender" type="hidden" name="Gender"/> 
    </div> 
    </label> 
    <span id="errormsg_0_Gender" class="errormsg" role="alert">Это поле должно быть заполнено.</span> 

+0

相同的代碼工作正常,因爲我在鉻合金中試過 – NarendraR

回答

0

我通過重載Selenium WebDriver 3.4.0 + Firefox 53.0(64位)來解決這個問題。在此之前,我有WebDriver 2.53.1 + Firefox 47.0.2。可能這個問題與環境有關,因爲代碼是真實的。 謝謝你的回答。

-1

相反,你可以彎曲你的WebElement到選擇對象和使用方法如下所示:

Select dropdown = new Select(driver.findElement("Your locator goes here")); 
dropdown.selectByVisibleText("Мужской"); 

您可以使用selectByValue ()或selectByIndex而不是以前的方法,如果你願意。

編輯:

我已經看到您正在處理的頁面和修改你的代碼。

首先,點擊下拉:

driver.findElement(By.cssSelector("#Gender > div > div")).click(); 

然後,調用方法如下:

selectItemFromList(By.cssSelector("#Gender > div[class='goog-menu goog-menu-vertical'] > div > div"), "Your item name goes here"); // you can any locator here for other dropdown menu 

這裏就是你前面提到的那種方法實現:

static void selectItemFromList(By element, String itemName){ 
      List<WebElement> itemList = driver.findElements(element); 

      for(WebElement item : itemList){ 
       if(item.getText().trim().equals(itemName)){ 
        item.click(); 
        break; 
       } 
      } 
     } 
+1

沒有選擇標籤。由OP提供的代碼在我的最後工作正常,因爲我已經在Chrome中嘗試了 – NarendraR

+0

那麼您能提供HTML部分嗎? –

+0

是的,正如Narendra Rajput所說,Select不在這裏工作。 – PrinceOFF

0

其實你的代碼正常工作 我已經在我的環境中試過了,文本是從我已經改變了下拉列表中正確 的唯一的事情是,我添加了路徑壁虎司機

這裏是我的機器上工作的編輯的代碼:

public class test1 { 
    static WebDriver driver; 

public static void main(String[] args) { 
    System.setProperty("webdriver.gecko.driver", "C:\\Eclipse\\geckodriver\\geckodriver.exe"); 
    driver = new FirefoxDriver(); 
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
    driver.manage().window().maximize(); 

    driver.get("https://accounts.google.com/SignUp?service=mail&hl=ru&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fpc%3Dtopnav-about-ru"); 

    driver.findElement(By.xpath("(//strong[text()='Пол']/following-sibling::div/div[@role='listbox'])[1]")).click(); 
    driver.findElement(By.xpath("//div[text()='Мужской']/parent::div[@role='option']")).click(); 
} 
} 

我使用壁虎驅動程序:geckodriver-v0.14.0-Win64的

我的Firefox瀏覽器版本爲:51.0.1

所以問題可能與您的環境

+0

是的,謝謝你的回答。我剛剛看到這個,今天早上已經重新安裝webdriver 。 – PrinceOFF