spandan madan
spandan madan

Reputation: 319

Selenium clicking button with no name or id

I've been struggling with trying to automate this page. After I login, I'm at this page where I'm supposed to click a button before I redirects me to the next page. The problem is that this button does not have a name or an ID which is making it very difficult to find this element. I've tried mechanize and splinter both. And finally tried selenium but that didn't help either. Really struggling with this. Just need to click this damn button! Any help would be really really appreciated. Love python and automation, but this time nothing seems to be working for me. Please find below a snapshot showing the the code shown when I click on "inspect element". Also, I can't type here the page source code as it is >300000 characters, so you can probably take a look at the page (you'll need to login which takes just 10 seconds). The page I'm referring to is right after you login - http://www.160by2.com/Index

[!Snapshot showing the code I get when I click "inspect element" [][1]]1

Upvotes: 3

Views: 4246

Answers (5)

spandan madan
spandan madan

Reputation: 319

Thank you so much for your replies! I was actually able to make it work using splinter instead of selenium! Here's what I did-

1) I just executed the js which was being executed on the onclick event of the button.

jsstring="window.parent.openPage('SendSMS?id="+id+"', 'aSendSMS', 'aSMS', 'ulSMS')"
br.execute_script(jsstring)

2) Subsequently, I was faced with the problem that on the next page, everything was embedded inside iframe, and so find_by_name and all were not able to find elements. For that, splinter provides a nice method (Which is documented REALLY bad, so had to figure it out myself with help from stackoverflow)

with br.get_iframe('iframe_Name') as iframe:
     iframe.fill("Element_to_fill_Name","Text_to_fill")
     iframe.find_by_tag("TagName")[Index_number].fill("Text_To_Fill")

Worked out brilliantly! Thanks everyone :)

Upvotes: 0

Slav Kurochkin
Slav Kurochkin

Reputation: 424

There is class name:

driver.findElement(By.className("da-sms-btn").

Also you can open application in Chrome and copy CSS or XPATH in browser:

  1. Open application in Chrome
  2. Inspect element
  3. Right click on highlighted area
  4. Copy CSS or XPATH

Upvotes: 3

Saifur
Saifur

Reputation: 16201

I would write a cssSelector as follows and try that.

button[onclick*='aSMS']

Notice, I am doing a partial search with *

Upvotes: 1

Jonathan
Jonathan

Reputation: 41

You can try to find the element through its xpath. Selenium does a good job of this:

webdriver.find_element_by_xpath("element xpath").click()

You can easily find the xpath by going to the inspect element sidebar on Chrome and right clicking on the element you want. The right click drop down menu should have an option "copy xpath".

Upvotes: 1

francisco sollima
francisco sollima

Reputation: 8338

You can try first getting the form by the id, then getting the button by the class name:

wd.find_element_by_id("frmDashboard").find_element_by_class_name("da-sms-btn").click()

Upvotes: 1

Related Questions