简体   繁体   English

如何使用Java在Selenium中按Delete键?

[英]How to press the delete button in selenium using java?

I have try to press the delete key using below command but it doesn't reflect anything! 我尝试使用下面的命令按Delete键,但它没有任何反应!

element = driver.findElement(By.cssSelector((#DeleteThis)));
element.sendKeys(Keys.DELETE);

You need to build an action and the send it, the send keys is bound to actions not elements, example : 您需要构建一个动作并将其发送,发送键绑定到动作而不是元素,例如:

Actions action = new Actions(yourDriver);

action.sendKeys(Keys.DELETE).build().perform();

This will simulate pressing "delete" from your keyboard 这将模拟从键盘上按“删除”

In case you have the element delete (as from your example) and you need to click it, you just perfomr the click action on the element. 如果您删除了元素(如您的示例所示)并且需要单击它,则只需对元素执行click操作。

Ok to do so, you need to use the delete command, kindly use the below code to perform the same. 好的,您需要使用delete命令,请使用以下代码执行相同的操作。 If it doesn't work first try to select it, as mentioned below. 如果不起作用,请先尝试将其选中,如下所述。

To delete it use below code: 要删除它,请使用以下代码:

 WebElement ele = driver.findElement(By.cssSelector("#DeleteThis"));

 ele.sendKeys(Keys.chord(Keys.DELETE));

OR 要么

Have you tried to first select it and then delete it? 您是否尝试过先选择它然后删除它?

Use below code, it will first select the element and then delete it. 使用以下代码,它将首先选择元素,然后将其删除。

ele.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM