动作点击脚本-Selenium [英] Actions click script - Selenium

查看:89
本文介绍了动作点击脚本-Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是我使用Selenium IDE创建的一个Selenium脚本,我试图从技术按钮(应单击教育"标签)中单击250个像素.通过运行脚本,您可以看到高亮显示了教育"选项卡,就像将鼠标悬停在该选项卡上一样,但控制台显示错误:

The code below is a selenium script I created with the Selenium IDE, I'm trying to get it to click 250 pixels from the technology button which should click the 'education' tab. You can see by running the script that the 'education' tab is highlighted as if its moused over but the console gives the error:

org.openqa.selenium.WebDriverException:无法在以下位置单击元素 点(753.5,107.51666259765625).其他元素将收到 点击

org.openqa.selenium.WebDriverException: Element is not clickable at point (753.5, 107.51666259765625). Other element would receive the click

使用:

  • FireFox 45.0.0.1
  • Selenium Webdriver 2.53.1
    package MyPackage;

    import java.util.regex.Pattern;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import static org.hamcrest.CoreMatchers.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.firefox.internal.ProfilesIni;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.Select;

    public class BBC {
       private String baseUrl;
       private boolean acceptNextAlert = true;
       private StringBuffer verificationErrors = new StringBuffer();
    static ProfilesIni profile = new ProfilesIni();
    static FirefoxProfile ffprofile = profile.getProfile("selenium");
    static WebDriver driver = new FirefoxDriver(ffprofile);

       @Before
        public void setUp() throws Exception {
    baseUrl = "https://www.google.co.uk/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }

      @Test
       public void testBBCtest2() throws Exception {
    driver.get(baseUrl + "/?gfe_rd=cr&ei=h4qDV93mBOjR8gfVi4qwDg&gws_rd=ssl");
    driver.findElement(By.id("lst-ib")).clear();
    driver.findElement(By.id("lst-ib")).sendKeys("BBC news");
    driver.findElement(By.linkText("Home - BBC News")).click();
    driver.findElement(By.xpath("//div[@id='site-container']/div/div[2]/ul/li[6]/a/span")).click();
    WebElement link = driver.findElement(By.xpath("//div[@id='site-container']/div/div[2]/ul/li[6]/a/span"));
    Actions builder = new Actions(driver);   
    builder.moveToElement(link, 250, 0).click().build().perform();
     }

     @After
      public void tearDown() throws Exception {
      String verificationErrorString = verificationErrors.toString();
      if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
       }
     }

     private boolean isElementPresent(By by) {
       try {
      driver.findElement(by);
      return true;
       } catch (NoSuchElementException e) {
      return false;
      }
     }

     private boolean isAlertPresent() {
      try {
      driver.switchTo().alert();
      return true;
       } catch (NoAlertPresentException e) {
      return false;
       }
      } 

     private String closeAlertAndGetItsText() {
       try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
      } finally {
      acceptNextAlert = true;
     }
     }
     }

推荐答案

而不是:

builder.moveToElement(link, 250, 0).click().build().perform();

尝试:

builder.moveToElement(link, 250, 0);
builder.clickAndHold();
builder.release();
builder.build();
builder.perform();

这篇关于动作点击脚本-Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆