我正在尝试填写表格来创建 Gmail 帐户。
package seleniumpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class HoverTest {
public static void main(String[] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.gmail.com");
Thread.sleep(2000);
driver.findElement(By.linkText("Utwórz konto")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id='BirthMonth']/div[1]/div[2]")).click();
Thread.sleep(2000);
// Neither this works
//driver.findElement(By.xpath("//*[@id=':5']/div")).click();
// Nor that
WebElement HoverMonth = driver.findElement(By.xpath("//div[contains(@class, 'goog-menuitem-content') and text()='Maj']"));
Actions action = new Actions(driver);
action.moveToElement(HoverMonth).perform();
Thread.sleep(2000);
action.click(HoverMonth).perform();
}
}
问题是在下拉菜单中单击月份。
在第二种方法中,驱动程序正确地将鼠标悬停在特定月份上,但它不会单击它,这会导致“Miesiąc”而不是“Maj”。将 xpath 传递到特定月份也不起作用。
最佳答案
您必须先单击“生日”下拉列表,然后单击所需的月份。我刚刚尝试了这段代码,它有效。
driver.get("https://accounts.google.com/b/0/SignUp?service=mail");
driver.findElement(By.cssSelector("div[title='Birthday']")).click();
driver.findElement(By.id(":6")).click();
其中“:6”是第 6 个月。
关于java - 使用 Selenium/Java 单击下拉菜单中的 Web 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33289743/