簡體   English   中英

Selenium WebDriver和Java Robot類

[英]Selenium WebDriver and Java Robot Class

我想使用Java Robot類來將鼠標移到鏈接上以動態創建更多內容。 對於Web交互,我使用Selenium WebDriver。

    Point coordinates = driver.findElement(By.xpath("//li[@id='1234']/a")).getLocation();
    Robot robot;
    try {
        robot = new Robot();
        robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
    } catch (AWTException e1) {
        e1.printStackTrace();
    }

Selenium引發getLocation函數錯誤:

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot determine size of element

有人知道我在做什么錯嗎?

您可以在不使用Robot的情況下實現鼠標懸停動作( Actions類)。

new Actions(driver).moveToElement(driver.findElement(By.xpath("//li[@id='1234']/a"))).perform();

在文件中包含以下導入語句。

import org.openqa.selenium.interactions.Actions;

如果您只想在頁面上移動鼠標,Selenium交互可以幫助您完成此操作。

這是適合您的示例代碼

WebElement myLink = driver.findElement(By.xpath("//li[@id='1234']/a"));

Actions act = new Actions(driver);
act.moveToElement(myLink).build().perform();

// if you want to click on the link : 
act.click(myLink).build().perform();

// if you want to move to the element and then click onthe link : 
act.moveToElement(myLink).click(myLink).build().perform();

// or can be done in two different steps like this : 
act = act.moveToElement(myLink);
act.click(myLink).build().perform()

為此,我們應該導入org.openqa.selenium.interactions.Actions;。

希望這能解決您的問題。

我嘗試了這個,它似乎為我工作。 請檢查

Point p = webele.getLocation();
int x = p.getX();
int y = p.getY();
Dimension d = webele.getSize();
int h = d.getHeight();
int w = d.getWidth();
Robot r = new Robot();
r.mouseMove(x + (w/2), y+(h/2) +80);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM