首页 > 解决方案 > 如何使用 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();

标签: javaselenium

解决方案


不看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();

推荐阅读