【问题标题】:Actions class - click(WebElement ele) function does not clicksActions 类 - click(WebElement ele) 函数不点击
【发布时间】:2021-05-09 02:04:56
【问题描述】:

我正在尝试使用 Actions 类的 click(WebElement) 方法单击 google 主页上的元素。代码运行成功,但是点击事件没有触发。

package p1;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;



public class ClickLink 
{
    static WebDriver driver;
    public static void main(String[] args)
    {
        try
        {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.google.com/");
        WebElement icon = driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a"));
        Actions ob = new Actions(driver);
        ob.click(icon);
        System.out.println("Link Clicked !!");
        Thread.sleep(10000);
        driver.close();
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred : "+e);
            driver.close();
        }
    }
}

这是执行上述脚本时的结果:[link]

但是,当使用 WebElement 接口的 click() 方法点击同一个元素时,就会触发点击。

package p1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;

public class ClickLink 
{
    static WebDriver driver;
    public static void main(String[] args)
    {
        try
        {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.google.com/");
        WebElement icon = driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a"));
        icon.click();
        System.out.println("Link Clicked !!");
        Thread.sleep(10000);
        driver.close();
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred : "+e);
            driver.close();
        }
    }
}

这是执行上述脚本时的结果:[link]

请告诉我点击事件未触发的原因和解决方法。

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    您犯了一个简单的错误,即不是buildingperforming 操作。 请注意,您已经创建了Actionsob 的实例。顾名思义,Actions 类定义了一组要执行的顺序操作。所以你必须build()你的动作来创建一个Action然后perform()这个动作。

    下面的代码应该可以工作!!

    WebElement icon = driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a"));
    Actions ob = new Actions(driver);
    ob.click(icon);
    Action action  = ob.build();
    action.perform();
    

    如果您查看下面给出的代码,首先移动到 icon 元素然后单击该元素会更好地解释 Actions 类。

    Actions ob = new Actions(driver);
    ob.moveToElement(icon);
    ob.click(icon);
    Action action  = ob.build();
    action.perform();
    

    【讨论】:

    • 谢谢西吉尔。你的建议对我有帮助。如果每个人都遵循 Java 约定并使用全名而不是缩写,这也会有所帮助。什么是ob?目的?这不是一个信息量很大的变量名。我会把它命名为动作。诚然,actions 和 action 看起来很相似,因此更具体地命名后者可能是个好主意;即clickAction。
    • 我使用了问题中提供的变量名称。我同意你的观点,如果我们所有人都使用描述性变量名,世界会变得更美好:)
    【解决方案2】:

    这是你需要做的:

    ob.click(icon).build().perform();
    

    你也可以这样做:

    ob.moveToElement(icon).click().build().perform();
    

    【讨论】:

      【解决方案3】:

      在网络自动化中,这个问题不断出现。原因可能是多方面的。让我们一一看看:

      1. 动作类未正确使用: Link to Official documentation

        正如方法文档所说,

      在方法链的末尾调用 perform() 以实际执行操作。

      使用Actions类实现点击的一般方法如下:

      actionsObj.moveToElement(element1).click().build().perform()
      
      1. 如果 Actions 类失败,有时原因可能是您收到以下异常:

        ElementNotInteractableException [object HTMLSpanElement] 没有大小和位置

      这可能意味着两件事:

      一个。元素未正确渲染:解决方案就是使用隐式/显式等待

      • 隐式等待:

        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

      • 显式等待:

        WebDriverWait 等待=新 WebDriverWait(驱动程序, 20); element1 = wait.until(ExpectedConditions.elementToBeClickable(By.className("fa-stack-1x")));

      b.元素已呈现,但它不在屏幕的可见部分:解决方案只是滚动到元素。根据 Selenium 的版本,它可以以不同的方式处理,但我将提供适用于所有版本的解决方案:

          JavascriptExecutor executor = (JavascriptExecutor) driver;
          executor.executeScript("arguments[0].scrollIntoView(true);", element1);
      
      1. 假设这一切都失败了,那么另一种方法是再次使用 Javascript 执行器,如下所示:

        executor.executeScript("arguments[0].click();", element1);

      2. 如果您仍然无法点击,那么这可能意味着两件事:

      1.内嵌框架

      检查 DOM 以查看您正在检查的元素是否存在于任何帧中。如果是这样,那么您需要在尝试任何操作之前切换到此帧。

          driver.switchTo().frame("a077aa5e"); //switching the frame by ID
          System.out.println("********We are switching to the iframe*******");
          driver.findElement(By.xpath("html/body/a/img")).click();
      

      2。新标签

      如果打开了一个新选项卡并且该元素存在于其上,那么您需要再次编写如下代码以在尝试操作之前切换到该选项卡。

      String parent = driver.getWindowHandle();
      driver.findElement(By.partialLinkText("Continue")).click();
      Set<String> s = driver.getWindowHandles();
      // Now iterate using Iterator
      Iterator<String> I1 = s.iterator();
      while (I1.hasNext()) {
      String child_window = I1.next();
      if (!parent.equals(child_window)) {
          driver.switchTo().window(child_window);
          element1.click() 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多