java 在WebDriver中点击webElement有几种方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35770992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:34:17  来源:igfitidea点击:

How many ways to click on webElement In WebDriver?

javaseleniumselenium-webdriver

提问by SacTan

As I am aware that user can click on Particular Webelement by using click method and one more way like using Sendkey Method with ASCII Value for left Click.

据我所知,用户可以通过使用单击方法和另一种方法来单击特定的 Webelement,例如使用带有 ASCII 值的 Sendkey 方法进行左键单击。

By Click Method: driver.findElement(By.cssSelector(".dbl")).click();

By Ascii Value : driver.findElement(By.cssSelector(".dbl")).sendKey("ASCII VALUE FOR Left Click");

Apart from this is there a way to perform click action??

除此之外有没有办法执行点击动作??

回答by Prateek

You can use:

您可以使用:

yourelement.sendKeys(Keys.RETURN)or .sendKeys(Keys.ENTER): which is an equivalent of focusing that element and hitting RETURN/ENTER on that element

yourelement.sendKeys(Keys.RETURN).sendKeys(Keys.ENTER): 这相当于聚焦该元素并在该元素上按 RETURN/ENTER

Also, There are methods to do this using Javacript but it is not usually recommended:

此外,有使用 Javacript 执行此操作的方法,但通常不推荐使用:

using the non-native Javascript Executor:

使用非原生 Javascript Executor:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", yourelement);

((JavascriptExecutor) driver).executeScript("arguments[0].click();", yourelement);

or by using Javascript Library:

或使用 Javascript 库:

JavascriptLibrary jsLib = new JavascriptLibrary();`
jsLib.callEmbeddedSelenium(driver, "triggerMouseEventAt", we, "click", "0,0");

回答by Pankaj Dubey

submit(); If the current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded

提交(); 如果当前元素是一个表单或表单中的一个元素,那么这将被提交到远程服务器。如果这会导致当前页面发生变化,则此方法将阻塞,直到加载新页面

回答by Ramakrishna Jangatisetty

Below are some methods that will be useful to click a button/Image.

以下是一些有助于单击按钮/图像的方法。

WebDriver driver = new ChromeDriver();
    driver.get("http://newtours.demoaut.com");
    WebElement signOnImage = driver.findElement(By.xpath("//input[@type='image'][@name='login']"));

// direct method from the API which is recommended always
    signOnImage.click(); 

1 Using Return Key

1 使用回车键

    //signOnImage.sendKeys(Keys.RETURN); 

2 Using JavascriptExecutor

2 使用JavascriptExecutor

2.1 
    JavascriptExecutor js = (JavascriptExecutor)driver; 
    js.executeScript("arguments[0].click();", signOnImage);

2.2         
    JavascriptExecutor js = (JavascriptExecutor) driver; 
    js.executeScript("document.getElementsByName('login')[0].click()");

3 Using Actions class

3 使用动作类

3.1
    Actions actions = new Actions(driver);  
    actions.click(signOnImage).perform();
3.2
    Actions actions = new Actions(driver);
    actions.moveToElement(signOnImage).click().perform();
3.3 
    Actions actions = new Actions(driver); 
    actions.clickAndHold(signOnImage).release().perform();
3.4 
    Actions actions = new Actions(driver);
    actions.sendKeys(signOnImage, Keys.RETURN).perform();

回答by Ranjith

If you want to click a button or set an value to an web element through selenium you can use XPATHvariable ,for using XPATH variable you must find the value of it ,you can find it using Firefox browser and few add_on's like firebugs .

如果你想点击一个按钮或通过 selenium 为 web 元素设置一个值,你可以使用XPATH变量,使用 XPATH 变量你必须找到它的值,你可以使用 Firefox 浏览器和一些 add_on 找到它,比如 firebugs 。

driver.findElement(By.xpath(".//*[@id='main']/div[4]/div/button")).click();

I would suggest you to use XPATH variable so that you can locate any web element in a webpage.

我建议您使用 XPATH 变量,以便您可以在网页中找到任何 Web 元素。

If you want find the hyperlink web element then you can use By.linkTextwhen you are sure about the tag name or choose By.partialLinkTextby which you can locate even if you partial web element name but in this case your partial search key matches more than one element then By.partialLinkText won't works fine. For example,in case you knows the complete tag name of hyperlink use can use

如果您想找到超链接网页元素,那么您可以在确定标签名称时使用By.linkText或选择By.partialLinkText即使您部分网页元素名称也可以通过它来定位,但在这种情况下,您的部分搜索键匹配更多超过一个元素,那么 By.partialLinkText 将无法正常工作。例如,如果您知道超链接使用的完整标签名称可以使用

driver.findElement(By.linkText("Click to Next Page")).click();

or else

要不然

Where you knows only a partial tag name

您只知道部分标签名称的地方

driver.findElement(By.linkText("Next Page")).click();

The secound option will not help you in all instances.

在所有情况下,第二个选项都不会帮助您。