动作类-click(WebElement ele)函数不会单击 [英] Actions class - click(WebElement ele) function does not clicks
问题描述
我正在尝试使用Actions类的 click(WebElement)方法单击google主页上的元素.该代码成功运行,但click事件未触发.
I am trying to use the click(WebElement) method of the Actions class to click on an element on the google homepage. The code runs successfully but the click event is not trigerred.
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();
}
}
}
执行上述脚本时,结果如下: [链接]
Here is the result when the above script is executed : [link]
但是,当使用WebElement界面的click()方法单击同一元素时,则会触发该点击.
However, when the same element is clicked using the click() method of the WebElement interface, then the click is trigerred.
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();
}
}
}
执行上述脚本时,结果如下: [链接]
Here is the result when the above script is executed : [link]
请让我知道为什么不会触发click事件并为其解决的原因.
Please let me know the reason as to why the click event is not trigerred and resolution for the same.
推荐答案
您犯了一个简单的错误,即没有building
和performing
该操作.
请注意,您已经创建了Actions
类ob
的实例.顾名思义,Actions
类定义了一组要执行的顺序动作.因此,您必须build()
您的操作才能创建单个Action
,然后创建perform()
操作.
You have made a simple mistake of not building
and performing
the Action.
Note that you have created an instance of Actions
class ob
. As the name signifies the Actions
class defines a set of sequential actions to be performed. So you have to build()
your actions to create a single Action
and then perform()
the action.
下面的代码应该可以工作!
The below code should work!!
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
类.
If you look at the code given below to first move to the icon
element and then click the element would better explain the Actions
class.
Actions ob = new Actions(driver);
ob.moveToElement(icon);
ob.click(icon);
Action action = ob.build();
action.perform();
这篇关于动作类-click(WebElement ele)函数不会单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!