gpt4 book ai didi

java - 在菜单栏中选择特定元素

转载 作者:行者123 更新时间:2023-12-02 10:19:49 25 4
gpt4 key购买 nike

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);







}

}

在菜单栏中,我在向下滚动栏中有一个元素列表没有移动。但是,我想选择特定元素是区域(设置)。我已经尝试了下面的代码。但是,在线程“main”java.lang.ClassCastException中显示为异常..请给我代码enter image description here

最佳答案

尝试下面的代码:

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。如果您愿意,可以更改它。

上面的代码将登录到应用程序,然后将鼠标悬停在设置上,然后单击区域。

关于java - 在菜单栏中选择特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54416340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com