【发布时间】:2017-08-17 05:17:24
【问题描述】:
我是 Selenium WebDriver 的初学者,我正在 StackOverflow 主页上使用这个测试。这些将是我的测试步骤:
- 首先,进入 StackOverflow 的首页。
- 然后,将鼠标移动到 Users 按钮上,使其成为焦点。 (我们不必点击它,只需将鼠标悬停在它上面。)
- 现在,找到屏幕上的活动元素(即 当前焦点),然后点击它。
我希望单击“用户”按钮,因为它当前处于焦点位置,但没有发生。而是点击问题按钮。 这是我进行相同测试的代码。
package insertCartridge;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\SELENIUM\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
String baseUrl = "https://stackoverflow.com/";
driver.get(baseUrl);
driver.manage().window().setSize(new Dimension(1920, 1080));
WebElement Users = driver.findElement(By.xpath("/html/body/header/div/div[1]/nav/ol/li[5]"));
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(Users).perform();
Thread.sleep(3000);
driver.switchTo().activeElement().click();
}
}
我无法理解为什么我没有得到预期的输出。请帮忙。
【问题讨论】:
-
你不想点击用户?
-
@PotnuruRavi,是的,我不想点击用户。只需将鼠标光标移到它上面,使其聚焦,然后找到活动元素(应该是 USERS),然后单击它。
-
定位元素(使用坐标)并使用可以移动到“用户”选项卡的操作。动作生成器 = 新动作(驱动程序); builder.MoveToElement(WaitForElement(By.CssSelector(starElement)), location.X, location.Y);
-
抱歉,我是初学者,我在理解您的代码时遇到了问题,我对此毫无疑问。这里的starElement 是什么? WaitforElement 是一个函数还是你的意思是 ExplicitWait?
-
starElement 是用户的定位器值,WaitForElement 是一个类。
标签: java selenium selenium-webdriver automated-tests focus