我正在尝试在gmail注册Web应用程序上使用selenium webdriver从生日月字段中提取文本(月份名称,如1月,2月......)。代码如下所示:
<label id="month-label" class="month">
<span id="BirthMonth" 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" aria-activedescendant=":0" title="Birthday">
<div class="goog-menu goog-menu-vertical" style="-moz-user-select: none; visibility: visible; left: 0px; top: -299.133px; display: none;" role="listbox" aria-haspopup="true">
<div id=":1" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div class="goog-menuitem-content">January</div>
</div>
<div id=":2" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div class="goog-menuitem-content">February</div>
</div>
<div id=":3" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div class="goog-menuitem-content">March</div>
</div>
<div id=":4" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div class="goog-menuitem-content">April</div>
</div>
<div id=":5" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":6" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":7" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":8" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":9" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":a" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":b" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":c" class="goog-menuitem" role="option" style="-moz-user-select: none;">
</div>
<input id="HiddenBirthMonth" name="BirthMonth" type="hidden">
</span>
</label>
我试过起诉xpath和其他元素定位器但是无法提取文本。当我使用Select类时,我收到错误cannot apply select with div
。请提出一些解决方案。
答案 0 :(得分:0)
以下是您必须做的事情: 月份项是Bootstrap Dropdown。因此,您必须遍历它才能获得列表。这是工作代码:
package Gmail;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class gmailLogin {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://gmail.com");
driver.findElement(By.xpath(".//*[@id='link-signup']/a")).click();
Thread.sleep(2000);
driver.findElement(By.id("BirthMonth")).click();
Thread.sleep(2000);
List<WebElement> month_menu = driver.findElements(By.xpath(".//div[@class='goog-menuitem-content']"));
for (int i=0;i<month_menu.size();i++)
{
WebElement element = month_menu.get(i);
String contents = element.getAttribute("innerHTML");
System.out.println("Values from dropdown is : "+contents);
}
driver.quit();
}
}
如果这有助于您,请告诉我。