Reputation: 79
Can we use Click() functionality along with sendKeys()?? I just read a drop-down value using xpath and now i need to click on the particular value i have read. Actually its possible to use in two steps. But is there any option to read and click in a single code??
Thanks, SK
Upvotes: 1
Views: 15941
Reputation: 94
Yes and No.
Yes you can if you use .then()
function.
No if you try inline function .click().sendKeys('1+1=3')
(Not Work)
You can do like this:
await $browser.waitForAndFindElement($driver.By.id('element_id'))
.then(async (e) => {
await e.click();
await e.sendKeys('1+1=3');
});
Upvotes: 1
Reputation: 11
Actions action = new Actions(driver);
WebElement MobileNumber = driver.findElement(By.xpath("yourxpath")); action.moveToElement(MobileNumber).click().sendKeys("your text").build().perform();
Upvotes: 1
Reputation: 2019
Kindly try with this. I have used Enter key as a substitute for clicking.
driver.findElement(By.xpath("xpath")).sendKeys("Talk-Talk",Keys.ENTER);
Hope this helps. Thanks.
Upvotes: 2
Reputation: 193308
Following the Java Docs
, click()
method returns void
as follows:
void click()
Similarly, sendKeys()
method also returns void
as follows :
void sendKeys(java.lang.CharSequence... keysToSend)
So as per best programming practice we must not try to club-up click()
method with sendKeys()
method or vice versa. It would be ideal to achieve the intended task in two separate steps.
Upvotes: 0
Reputation: 1403
If your requirement is to select some specific option in dropdown then use select class.
Go though this article for more info
But if you want to click on some element and then send some text, then you can user Action class.
WebElement wb = driver.findElement(By.xpath("your xpath"));
Actions action = new Actions(driver);
action.moveToElement(wb).click().moveToElement(wb,200, 0).sendkeys("text").build().perform();//you need to specify where you need to send text 200,0 is just as an example
Upvotes: 1