无法使用Selenium WebDriver在www.mail.com/int/中单击“撰写”按钮

时间:2018-04-03 20:20:31

标签: java selenium selenium-webdriver xpath webdriver

撰写按钮按下命令不起作用。在输出中,它显示无法定位元素。在撰写其展示第三方iframe,所以甚至尝试了iframe,但没有工作导航按钮。

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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class mail {
    public static void main(String[] args) throws InterruptedException  {

        System.setProperty("webdriver.gecko.driver", "D:\mozilla   driver\geckodriver.exe");
        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.mail.com/int/");
        driver.findElement(By.xpath(".//*[@id='login-button']")).click();
        driver.findElement(By.xpath(".//*[@id='login-email']")).sendKeys("rahulrahulxyz@mail.com");
        driver.findElement(By.xpath(".//*[@id='login-password']")).sendKeys("bangbang");
        driver.findElement(By.xpath(".//*[@id='login-form']/button")).click();
        Thread.sleep(8000);
        driver.findElement(By.xpath(".//*[@id='navigation']/ul/li[3]/a")).click();    //here is error
    }
}

2 个答案:

答案 0 :(得分:0)

在gmail中,按钮上有文本可用于通过XPath选择它。我认为在你自动化的网站上有类似的东西

以下是找到"撰写" gmail中的按钮:

driver.findElement(By.xpath(.//*[text()='COMPOSE']));

答案 1 :(得分:0)

要点击文本为撰写电子邮件的元素,一旦登录,您必须切换到包含所需元素的<iframe>,然后您应找到所需元素。您可以使用以下代码块:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.mail.com/int/");
driver.findElement(By.xpath(".//*[@id='login-button']")).click();
driver.findElement(By.xpath(".//*[@id='login-email']")).sendKeys("rahulrahulxyz@mail.com");
driver.findElement(By.xpath(".//*[@id='login-password']")).sendKeys("bangbang");
driver.findElement(By.xpath(".//*[@id='login-form']/button")).click();
Thread.sleep(8000);
driver.switchTo().frame("thirdPartyFrame_home");
driver.findElement(By.linkText("Compose E-mail")).click();

结果页面快照:

Compose E-mail