Java 在菜单栏中选择特定元素
在菜单栏中,我有一个元素列表,在该向下滚动条中没有移动。但是,我想选择特定的元素是区域(设置)。我已经尝试了下面的代码。但是,在线程“main”java.lang.ClassCastException中显示为异常..请编写代码尝试以下代码:Java 在菜单栏中选择特定元素,java,selenium,Java,Selenium,在菜单栏中,我有一个元素列表,在该向下滚动条中没有移动。但是,我想选择特定的元素是区域(设置)。我已经尝试了下面的代码。但是,在线程“main”java.lang.ClassCastException中显示为异常..请编写代码尝试以下代码: package com.s3sales.demo; import java.awt.AWTException; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExe
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);
}
}
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();
driver.get(“http://sssamriddhisales.com/crm");
Wait Wait=new FluentWait(驱动程序)。带超时(3,TimeUnit.MINUTES)。每隔(1,TimeUnit.SECONDS)轮询一次。忽略(NoSuchElementException.class,ElementNotVisibleException.class);
WebElement用户名=wait.until(新函数(){
公共WebElement应用(WebDriver){
返回driver.findElement(By.id(“用户名”);
}
});
WebElement password=wait.until(新函数(){
公共WebElement应用(WebDriver){
返回driver.findElement(通过.id(“密码”));
}
});
WebElement submit=wait.until(新函数(){
公共WebElement应用(WebDriver){
返回driver.findElement(By.xpath(//button[@type='submit']);
}
});
用户名。sendKeys(“管理员”);
密码。发送密钥(“admin123”);
提交。单击();
WebElement设置=等待.until(新函数(){
公共WebElement应用(WebDriver){
返回driver.findElement(By.xpath(//a[@class='text-center']///span[text()='Settings']);
}
});
动作动作=新动作(驱动);
actions.moveToElement(settings.build().perform();
最后一个WebElement=driver.findElement(By.xpath(//li[@data id='area']//a[text()='area']);
WebElement area=wait.until(新函数(){
公共WebElement应用(WebDriver){
返回元素;
}
});
((JavascriptExecutor)driver.executeScript(“参数[0]。ScrollingToView(true);”,元素);
区域。单击();
使用Thread.sleep()不是一个好主意,因此我将其删除,并用FluentWait替换。如果你愿意,你可以改变它
上述代码将登录到应用程序,然后将鼠标悬停在设置上,然后单击该区域。请尝试以下代码:
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);
}
}
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();
你能给我们看一些关于列表元素的html吗?你能告诉我们你在哪一行得到了例外吗?你能一步一步地解释代码吗(很抱歉问)没问题,我正在执行与你相同的步骤,但我已经改变了定位web元素的方式,并删除了Thread.sleep()。直到登录到应用程序,我想你的代码会有用的。之后,无需单击“设置”,因为它通过在自身上进行鼠标操作来显示列表,所以我使用“Actions”类将控件移动到那里,然后使用“JavascriptExecutor”获取元素视图,然后单击它。如果FluentWait不起作用,则使用Thread.sleep()谢谢你,兄弟,我已经在互联网上看到了所有的类和方法的解释…(如果你还好的话)你能提供你的id像Facebook、LinkedIn、Instagram之类的帮助吗…再次感谢你在代码方面的帮助…对不起,兄弟,它不起作用了Hi@Bhavesh Soni,这不是他想做的。他想在鼠标悬停在设置上后从左侧选择一些选项。不在右上角的管理设置…哦,对不起,我没有正确地检查它。更新了相同的代码。