Selenium clicking button with no name or id - javascript

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

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:
Open application in Chrome
Inspect element
Right click on highlighted area
Copy CSS or XPATH

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()

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".

I would write a cssSelector as follows and try that.
button[onclick*='aSMS']
Notice, I am doing a partial search with *

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 :)

Related

document.evaluate with XPath only working if I inspect element first

All new to JavaScript and experiencing a frustrating problem.
So basically I'm trying to achieve a click as part of an automation process. Here's what I'm doing:
document.evaluate('//a[#aria-label="Salgsfakturaer" and #role="menuitem"]',document.body,null,9,null).singleNodeValue.click()
This works great from the console but ONLY if I've gone to the trouble of inspecting the element first! If I reload the page and fire off the same I get an error. Again I can then go and inspect the element and try again - and it works!
So what I'm thinking, and I'm no JavaScript expert, is that it has something to do with the main content being in an iframe and that the source is not loaded initially (only when I go to inspect). If this is a correct assumption, how can I handle this?
Thanks in advance for pointing me in the right direction.

Javascript button click not working in selenium webdriver

I am trying to automate hitting a javscript button using a python script and web driver however no matter how I try to refer to the element it doesn't seem to activate the javscript. Here's an excerpt from the website I am trying to hit the button on:
<li>
<a href="javascript:;" data-blogid="19079" id="picture-trigger">
<i class="glyphicon glyphicon-picture light-red"></i >
<span> Picture </span >
</a >
</li >
I've tried selecting by CSS selector, by XPATH and while I see the element being selected (doted line around it when running), nothing happens.
I've also tried both .click() and .submit() neither one seems to work. Here's my most recent attempt:
element = mydriver.find_element_by_id("picture-trigger")
element.click()
I think perhaps the issue is the javascript:; isn't being triggered when called from web driver the same way it is when an interactive user hits it but I'm at a loss for how else to automate clicking it.
Does anyone with more web driver experience know why this isn't working or how I can get this working?
Thanks
Brad
Here's what I see when I run the code:
Here's what it looks like when I manually click on the button:
I think the issue lies in the fact that the site is using jquery. I was able to work around this using the following:
time.sleep(3);
mydriver.execute_script("$('.light-red').click();")
time.sleep(3);
mydriver.execute_script("$('.uploadbutton').click();")
There may be a better way than using time.sleep but I spent so much time fighting with trying to get the div to unhide that its good enough for now.
Hopefully this helps someone else out in a similar situation. Thanks to everyone for all the help.
Here is how I find my way to click on an element using XPath:
Browse in the target page where the button is located(Tried with
FireFox).
Right click on the button and select Inspect elements.
Right click on the HTML code of the button and in Copy select XPath.
Now you have the XPath copied to your clipboard.
Now you just have to call this line:
document.getElementByXPath("PASTE THE XPATH HERE").click()

How can I use inspect elements on Chrome to identify the source of a particular section of a website?

This may sound like a really stupid question for you guys, but how can I use the inspect elements tool on Chrome to identify the source of a particular section of a website? To make it simpler to understand, I want to modify the footer of a particular website (WordPress based).
The problem is that the footer was customized (which means I can’t edit through the WP backend) so I need to find where exactly or what’s generating this footer.
Does this make any sense to you?
I'm pretty sure you can't do this in Developer tools as wordpress is PHP based, chrome only sees the compile code.
What I would do in your situation is to try searching the theme folder for an ID, Class or piece of code that is unique to the footer and try that way.
This can be done by visting the website, right clicking and selecting Inspect.
The developer tools dropdown will appear. In the top left corner there is an icon with a pointer hovering over a screen (this is the inspect element tool). Click this, and move your pointer on to the part of the webpage you wish to find the HTML for.
The html for that part of the website will be highlighted in the developer window, making it easy for you to see.
You can then either , edit inline in the browser to get an immediate view of the changed code (this will not be permanent, or change you code file), or use that location, to return to your WP editor and make permanent changes.

Having issues executing multiple javascript lines in chrome console

I have been trying to do some manipulation on webpages using JavaScript. So I started with the basic Google page(https://google.com).
I ran the command
document.getElementById('lst-ib').value = 'search_term';
Then after that tried running
document.getElementsByName('btnI')[0].click();
which is the I'm feeling Lucky button.
It is showing undefined on doing so.
But when I try only click on the I'm Feeling Lucky button without changing the content of the search bar, it works.
The target is to click the Search Button, it is also not working.
So I used the I'm Feeling Lucky button for testing to see if anything can be clicked.
The Same is happening in YouTube search bar as well
Any help or guidance would be much appreciated.
Also please note that I am N00b and trying to learn as I go :(
TLDR; The Idea is to simulate all the operation a user can do on the browsers using JavaScript.
document.getElementById('lst-ib').value = 'search_term';
document.getElementById('tsf').submit();
(or document.querySelector("form").submit();)
I'm not entirely sure what you're trying to achieve, but did you consider just using the queryparams? google.com?search=search_term ?
I found this ID simply by going to google.com, rightclicking on the input field, choose 'inspect element' and search for the first ancestor that is a 'form'.

How do I make a desktop shortcut which links to a specific element of a webpage?

I'm trying to create a shortcut to a search bar on a website. After this shortcut has been created I am going to give it a keyboard shortcut.
How do I focus on a specific element on a page when it is opened for the first time in a browser?
I have researched bookmarklets, but I don't think they do what I want them to do.
I also had a look at javascript, and I have a feeling I will need to use:
document.findElementByName("").focus()
Can anyone help? Thanks.
EDIT: It's worth noting it is not my webpage. It is a public webpage.
Assuming the element has an id, say importantElement, then it should be sufficient to add #importantElement to the URL string.

Categories