How to click on a button using the selenium webdriver

2,115 views
Skip to first unread message

MCD dotCom

unread,
Jul 20, 2020, 8:18:55 PM7/20/20
to Selenium Users
Hello,

HTML:

<button class="Button__ButtonWithStyles-y45r97-0 styles__StyledButton-sc-1cl7zs9-0 hhxwTy" data-test="shipItButton">Ship it</button>

I tried the following and nothing has worked. 

         driver.findElement(By.xpath("//button[@class='Button__ButtonWithStyles-y45r97-0']")).click();
         driver
.findElement(By.className("Button__ButtonWithStyles-y45r97-0")).click();
         driver
.findElement(By.id("Ship it")).click();
         driver
.findElement(By.name("Ship it")).click();



org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:
 

Please advise! 

Josh Abrahamsen

unread,
Jul 20, 2020, 8:34:47 PM7/20/20
to seleniu...@googlegroups.com
Try css selector of

[data-test='shipItButton'

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/89c2c5d6-444f-469a-9501-56ab2bea724ao%40googlegroups.com.

Hanumantha Reddy

unread,
Jul 21, 2020, 12:30:23 AM7/21/20
to Selenium Users
You can try below statement,

driver.findElement(By.xpath("//button[@data-test='shipItButton']")).click();

Follow below video for more details on how to use attributes in the xpath.


Disclaimer: I support this youtube channel

bharath kumar

unread,
Jul 21, 2020, 2:00:47 AM7/21/20
to seleniu...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

sruthi reddy

unread,
Jul 22, 2020, 1:14:30 AM7/22/20
to seleniu...@googlegroups.com
Hi everyone,
Any one idea about this?
Actually I have known to little bit of handling DROP down s. 
Any one idea about this pls send the code.

This below code I know.... How to store values to array

WebElement we=driver. FindElement (By.xpath(""));
Select sel =new Select (we) ;
List<WebElement > values=sel.getOptions();
For(WebElement web:values) 
{
System.Out.Println (web.getText());
}

Vivek Zambre

unread,
Jul 22, 2020, 1:14:30 AM7/22/20
to Selenium Users
. click method

Nandeeswar Porla

unread,
Jul 22, 2020, 1:14:59 AM7/22/20
to Selenium Users
Hi,

  Use the below line of code to identify your button.

driver.findElement(By.xpath(//button[text()='Ship it'])).click()

or

driver.findElement(By.xpath(//button[contains(text(),'Français')]])).click()


Regards

Nandeeswar P N

MCD dotCom

unread,
Jul 22, 2020, 4:29:22 PM7/22/20
to Selenium Users
Hello, 

This code worked. 

driver.findElement(By.xpath(//button[text()='Ship it'])).click()


Once I click Ship it, I get a pop up with two buttons. One button is decline coverage. I used the similar code but it says "no such element..."

<div class="Col-favj32-0 hsLYrj h-padding-l-tiny h-padding-r-tiny"><button data-test="espModalContent-declineCoverageButton" type="button" class="Button-bwu3xu-0 gHLRCa h-margin-t-tight">Decline coverage</button></div>



Tried but no luck:

 
  driver.findElement(By.xpath("//button[text() = 'Decline coverage']")).click();
   driver
.findElement(By.xpath("//button[@data-test='Decline coverage']")).click();

Thanks!
t1.png

MCD dotCom

unread,
Jul 22, 2020, 5:26:28 PM7/22/20
to Selenium Users
This code prints the message but does not click. Please advise!

  driver.findElement(By.className("Button-bwu3xu-0")).click();
         
System.out.println("clicked");

Josh Abrahamsen

unread,
Jul 22, 2020, 5:37:17 PM7/22/20
to seleniu...@googlegroups.com
Because this is a div not a button, replace //button with //div

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

Josh Abrahamsen

unread,
Jul 22, 2020, 5:38:04 PM7/22/20
to seleniu...@googlegroups.com
Whoops, rad that wrong nevermind, this could be in an iframe or something like that. Hard to tell without more HTML.

On Wed, Jul 22, 2020, 3:29 PM MCD dotCom <mycheap...@gmail.com> wrote:
--

MCD dotCom

unread,
Jul 22, 2020, 6:33:43 PM7/22/20
to Selenium Users
Actually, I am playing with target.com.
Please go to the link
Then please click Ship it
It will display a dialog box which has two buttons. I like to click Decline button.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

roshan pote

unread,
Jul 23, 2020, 2:51:12 AM7/23/20
to Selenium Users
Hi, You can use action class or javaScriptExecutor  to click on button


On Tuesday, 21 July 2020 05:48:55 UTC+5:30, MCD dotCom wrote:
Reply all
Reply to author
Forward
0 new messages