这就是我在Gmail上尝试做的事情。
我可以点击第一封电子邮件并选择电子邮件地址,然后点击撰写电子邮件按钮,也可以发送电子邮件。 我面临的问题是无法单击设置图标。元素被隐藏,我无法点击它。我尝试使用自定义Xpath,并尝试用坐标单击它。 但它不适合我。请允许任何人帮助我。
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
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 Gmail1 {
public static void main(String[] args) throws InterruptedException {
// Login to browser
WebDriver driver= new FirefoxDriver();
driver.get("https://www.gmail.com");
driver.manage().window().maximize();
System.out.println("Browser opned");
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("Use your UserId");
System.out.println("Entered Email id");
driver.findElement(By.xpath("//*[@id='next']")).click();
System.out.println("Clicked on Next");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("Use your password");
System.out.println("Entered the Password");
driver.findElement(By.xpath("//*[@id='signIn']")).click();
System.out.println("Welcome to gmail");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id=':3d']")).click();
System.out.println("Clicked on email");
Thread.sleep(3000);
String emailid = driver.findElement(By.xpath("//span[@class='go']")).getText();
emailid=emailid.substring(emailid.indexOf("<")+1, emailid.indexOf(">"));
System.out.println(emailid);
driver.findElement(By.xpath("//*[@id=':it']/div/div")).click();
System.out.println("Clicked on Compose mail");
driver.findElement(By.xpath("//*[@name='to']")).sendKeys(emailid);
System.out.println("Entered the TO Email Address");
driver.findElement(By.xpath("//*[@name='subjectbox']")).sendKeys("My Mail");
System.out.println("Entered Subject of the email");
driver.findElement(By.xpath("//*[@role='button' and .='Send']")).click();
System.out.println("Clicked on send button");
clickSetting(driver);
}
public static void clickSetting(WebDriver driver){
//Tried with Coordinates (doesn't work)
Point point = driver.findElement(By.xpath("//div[@class='G-Ni J-J5-Ji'] [@gh ='s']/*[1]")).getLocation();
System.out.println(point.x + "-" + point.y);
Actions builder = new Actions(driver);
builder.moveByOffset(point.x, point.y).click().build().perform(); //Getting Error.
//Tried with Action Class (doesn't work)
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement SettingWheel=driver.findElement(By.xpath("//*[@data-tooltip='Settings' and @role='button']"));
WebElement SettingsLink=driver.findElement(By.xpath("//*[@role='menuitem']/div[.='Settings']"));
wait.until(ExpectedConditions.elementToBeClickable(SettingWheel));
Actions actions = new Actions(driver);
actions.moveToElement(SettingWheel).moveToElement(SettingsLink).click().build().perform();//Getting Error.
Thread.sleep(2000);
System.out.println("Clicked On Setting");
}
错误消息: - “元素当前不可见,因此可能无法与之交互(警告:服务器未提供任何堆栈跟踪信息)”
先谢谢。
答案 0 :(得分:1)
试试这个: -
public static void clickSetting(WebDriver driver){
List<WebElement> elements=driver.findElements(By.xpath("//div[@gh='s']/*[@role='button']"));
for(WebElement element:elements){
if(element.isDisplayed()){
element.click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id='ms']")).click();
Thread.sleep(5000);
}
}
答案 1 :(得分:0)
问题在于你的xpath。我尝试使用id广告。 Thread.sleep
是个坏主意。不要使用{而是使用WebDriverWait
无论如何,以下代码片段会点击设置齿轮然后设置并等到设置面板完全加载
同时浏览此Gmail Selenium,了解我如何在没有Thread.sleep的情况下处理Gmail登录
public static void clickSetting(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=':3d']"))).click();
System.out.println("Clicked On Settings Gear");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@role='menuitem']/div[.='Settings']"))).click();
System.out.println("Clicked On Setting");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='dt']")));
System.out.println("Settings Visible");
}
答案 2 :(得分:0)
试试这个:
package yourPackageName;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestClass2 {
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c:/chromedriver.exe");
driver = new ChromeDriver();
Step_1_LaunchApp();
Step_2_LoginUsingCredentials();
Step_3_ClickOnInbox();
Step_4_ClickOnFirstEmail();
Step_5_ClickOnComposeEmail();
Step_6_ClickOnSettingsIcon();
}
private static void Step_6_ClickOnSettingsIcon() {
try{
driver.findElement(By.xpath("//*[@class='aos T-I-J3 J-J5-Ji']")).click();
}catch(Exception e){
e.printStackTrace();
}
}
private static void Step_5_ClickOnComposeEmail() {
try{
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
}catch(Exception e){
e.printStackTrace();
}
}
private static void Step_4_ClickOnFirstEmail() {
try{
driver.findElement(By.xpath("//div[@role='tabpanel'][1]//table//tr[1]")).click();
}catch(Exception e){
e.printStackTrace();
}
waitTimeInSecond(2);
}
private static void Step_3_ClickOnInbox() {
driver.findElement(By.xpath("//span/a[contains(text(),'Inbox')]")).click();
waitTimeInSecond(2);
}
private static void Step_2_LoginUsingCredentials() {
driver.findElement(By.id("Email")).sendKeys("email@gmail.com");
driver.findElement(By.id("next")).click();
waitTimeInSecond(2);
driver.findElement(By.id("Passwd")).sendKeys("Password");
driver.findElement(By.id("signIn")).click();
waitTimeInSecond(5);
}
private static void Step_1_LaunchApp() {
driver.get("http://gmail.com");
}
public static void waitTimeInSecond(int waitTime){
try{Thread.sleep(waitTime*1000);}catch(Exception e){}
}
}
首先输入您的凭据并运行脚本,它将点击设置齿轮。