[英]Clicking the webelement in dropdown menu with Selenium / Java
我正在嘗試填寫表格以創建一個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傳遞到特定月份也不起作用。
與Xpath一起使用。
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class gmailSignup {
@Test
public void gmailCalendar(){
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://accounts.google.com/");
driver.findElement(By.partialLinkText("Utwórz konto")).click();
// Open the month select field
driver.findElement(By.xpath("//span[@id='BirthMonth']/div[1]/div[2]")).click();
// Select the fifth month = "Maj"
driver.findElement(By.xpath("//span[@id='BirthMonth']/div[2]/div[5]/div[1]")).click();
driver.quit();
}
}
您必須先單擊“生日”下拉菜單,然后單擊所需的月份。 我只是試過這段代碼,它的工作原理。
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個月。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.