package com.s3sales.demo;
import java.awt.AWTException;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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 Settings_Area {
public static void main(String[] args) throws InterruptedException, AWTException {
WebDriver driver=new FirefoxDriver();
driver.get("http://sssamriddhisales.com/crm");
Thread.sleep(1000);
driver.findElement(By.id("userName")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.id("password")).sendKeys("admin123");
Thread.sleep(2000);
driver.findElement(By.className("btn-success")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.linkText("Settings"));
WebDriverWait wait=new WebDriverWait(driver, 3);
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,100)");
wait.until(ExpectedConditions.elementToBeClickable(element));
Thread.sleep(1000);
Actions action = new Actions(driver);
action.moveToElement(element).moveToElement(driver.findElement(By.cssSelector("[data-id='area']"))).click().build().perform();
Thread.sleep(1000);
}
}
在菜单栏中,我在向下滚动栏中没有移动的元素列表。但是,我想选择特定元素为Area(Settings)。我尝试了下面的代码。但是,在线程“ main”中显示为异常java.lang.ClassCastException ..请给我代码enter image description here
答案 0 :(得分:1)
尝试以下代码:
driver.get("http://sssamriddhisales.com/crm");
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(3, TimeUnit.MINUTES).pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class, ElementNotVisibleException.class);
WebElement userName = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("userName"));
}
});
WebElement password = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("password"));
}
});
WebElement submit = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("//button[@type='submit']"));
}
});
userName.sendKeys("admin");
password.sendKeys("admin123");
submit.click();
WebElement settings = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("//a[@class='text-center']//span[text()='Settings']"));
}
});
Actions actions = new Actions(driver);
actions.moveToElement(settings).build().perform();
final WebElement element = driver.findElement(By.xpath("//li[@data-id='area']//a[text()='Area']"));
WebElement area = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return element;
}
});
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
area.click();
使用Thread.sleep()并不是一个好主意,因此我将其删除并替换为FluentWait。您可以根据需要进行更改。
上面的代码将登录到应用程序,然后将鼠标悬停在设置上,然后单击“区域”。
答案 1 :(得分:0)
请尝试以下代码:
driver.get("http://sssamriddhisales.com/crm");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
Thread.sleep(1000);
driver.findElement(By.id("userName")).sendKeys("admin");
Thread.sleep(1000);
driver.findElement(By.id("password")).sendKeys("admin123");
Thread.sleep(2000);
driver.findElement(By.className("btn-success")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.xpath("//a[@class='text-center']//span[text()='Settings']"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
driver.findElement(By.xpath("//li[@data-id='area']//a[text()='Area']")).click();