如何使用Selenium在“撰写”部分中单击更多选项?

时间:2018-12-16 03:57:53

标签: java selenium

我正在练习使用Selenium Java进行编码。

我单击“撰写”部分,然后输入“ 收件人,主题和正文”,然后尝试单击虚线以将标签选择为“社交”,但无法单击该部分。

请找到以下代码。

@Test
public void testSendEmail() throws Exception {
    driver.get("https://mail.google.com/");


    WebElement userElement = driver.findElement(By.id("identifierId"));
    userElement.sendKeys(properties.getProperty("username"));

    driver.findElement(By.id("identifierNext")).click();

    Thread.sleep(1000);

    WebElement passwordElement = driver.findElement(By.name("password"));
    passwordElement.sendKeys(properties.getProperty("password"));
    driver.findElement(By.id("passwordNext")).click();

    Thread.sleep(1000);

    WebElement composeElement = driver.findElement(By.xpath("//div[contains(text(),'Compose')]"));
    composeElement.click();
    WebDriverWait wait=new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//textarea[@name='to']")));
    //To Field
    driver.findElement(By.name("to")).clear();
    driver.findElement(By.name("to")).sendKeys(String.format("%s@gmail.com", properties.getProperty("username")));
    //Subject Field
    String emailSubject = properties.getProperty("email.subject");
    driver.findElement(By.xpath("//input[@name='subjectbox']")).sendKeys(emailSubject);
    //Body
    String emailBody = properties.getProperty("email.body"); 
    driver.findElement(By.xpath("//div[@class='Ar Au']//div")).sendKeys(emailBody);

        //More options----Line where I am unable to click on the dotted lines to mark label as social.

        driver.findElement(By.xpath("//div[@id=':q6']/div[2]")).click();
        Thread.sleep(2000);
        //Hover on Label
        WebElement Label=driver.findElement(By.xpath("(//div[contains(text(),'Label')])[1]"));
        WebElement Social=driver.findElement(By.xpath("//div[contains(text(),'Social')]"));
        Actions a=new Actions(driver);
        a.moveToElement(Label);
        Thread.sleep(5000);
        a.moveToElement(Social).click().build().perform();
        Thread.sleep(5000);



    //Click On Send
     driver.findElement(By.xpath("//*[@role='button' and text()='Send']")).click();

1 个答案:

答案 0 :(得分:0)

不看html很难回答。 代替:

Actions a=new Actions(driver);
a.moveToElement(Label);
Thread.sleep(5000);
a.moveToElement(Social).click().build().perform();

尝试一下:

a.moveToElement(Label).build().perform();
    waitUntil(ExpectedConditions.elementToBeVisible(Social)).click();