how can we click on a button using javascript in selenium and if the button does not have id

0 votes
Feb 20, 2019 in Selenium by anonymous
4,999 views
I'm not sure what role JavaScript plays, but I know for certain that there are other Locator techniques that can be used. They are by.name(), by.classname(), by.cssSelector(), by.xpath(), etc. Even if the by.id() is not present, one of these will definately be present. Do let me know if that's the case.

2 answers to this question.

0 votes

If button does not have id, you can write locator based on other locators strategies. 

For example here i am using xpath locator then you need to click button by using Java Script executor in following way...

JavascriptExecutor js = (JavascriptExecutor) driver;//create instance of javascript executor to do actions

WebElement nextButton = driver.findElement(By.xpath("//span[contains(text(),'Next')]"));

js.executeScript("arguments[0].click();", nextButton);

answered Feb 21, 2019 by nizam
0 votes
       WebElement webElement=driver.findElement(By.xpath(element));
        JavascriptExecutor js = (JavascriptExecutor) driver;

        js.executeScript("arguments[0].click();",webElement);  
answered Aug 31, 2020 by Sri
• 3,190 points

Related Questions In Selenium

0 votes
2 answers

How can we use JavaScript Executor to click and enter data to a web element in Selenium?

WebElement element = driver.findElement(By.id("abcd")); // Let the ...READ MORE

answered Mar 22, 2020 in Selenium by Lakshmi Sarvepalli
3,487 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,687 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,149 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,229 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,880 views
0 votes
3 answers

How to click the search button using Selenium web driver and Python

You can try with tag. #this code will ...READ MORE

answered Apr 10, 2019 in Selenium by Matin
16,250 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP