1

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: Selected Element

Here's what it looks like when I manually click on the button:

enter image description here

Brad
  • 1,979
  • 7
  • 35
  • 47
  • Hi.. I'm not sure I understand, the `href` action is a javascript no-op. What are you expecting to observe or have happen? – cody Dec 14 '18 at 18:58
  • CSS selector and xpath are two different ways of addressing an element. which one are you trying to use? – Omid N Dec 14 '18 at 19:00
  • @omidN I'm saying I've tried both approaches (using CSS selector *and* xpath) and neither one seems to work with .click() or .submit(). But I can see the right element is being selected so I don't understand why .click or .submit don't trigger the appropriate javascript. – Brad Dec 14 '18 at 19:08
  • @cody When I click the element interactively/manually a new window (i think its actually a
    or opens and the rest of the page is dimmed). But when I try to automate it with webdriver nothing happens. Its as if I haven't clicked anything.
    – Brad Dec 14 '18 at 19:09
  • To clarify it *is* a div (photobuttonholder). – Brad Dec 14 '18 at 19:12
  • Hope this help https://stackoverflow.com/questions/13040719/click-on-css-selector-not-working-in-selenium-webdriver – ahmet_y Dec 14 '18 at 19:20
  • Just to help debug this issue, you could change the javascript to something trivial yet visually obvious, like ``, and see if that works. – John Gordon Dec 14 '18 at 19:24
  • @JohnGordon I don't control the website I am trying to automate so I couldn't change it on the server but I did download a local copy, modify it and then point web driver to it. Doing that I confirmed that web drivers does produce a hello dialog box so it *is* hitting the button. It seems the issue isn't with targeting but that web drivers .click() method doesn't fire the same javascript that a real user does. – Brad Dec 14 '18 at 21:31

2 Answers2

2

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.

Brad
  • 1,979
  • 7
  • 35
  • 47
1

Here is how I find my way to click on an element using XPath:

  1. Browse in the target page where the button is located(Tried with FireFox).
  2. Right click on the button and select Inspect elements.
  3. Right click on the HTML code of the button and in Copy select XPath.
  4. Now you have the XPath copied to your clipboard.

Now you just have to call this line:

document.getElementByXPath("PASTE THE XPATH HERE").click()
Omid N
  • 947
  • 2
  • 11
  • 24
  • So this is python not javascript but converting that the equivalent would be: mydriver.find_element_by_xpath("/html/body/div[2]/section/div/div[18]/div/div[2]/div/ul/li[2]/a/i").click() and I do see my element selected (there's a dotted line around it) BUT the click action doesn't seem to be unhiding the photobuttonholder div. Thus my problem. – Brad Dec 14 '18 at 19:17
  • Is there a delay before making the click action? Maybe the page is still loading while the click is being issued. – Omid N Dec 14 '18 at 19:20
  • I've tried adding a delay with WebDriverWait(mydriver, 10) but it doesn't seem to make a difference. I think the issue is that javascript:; which to me looks null/empty. I don't understand how that even does anything. However as far as I can tell that's the only script on or around that element. – Brad Dec 14 '18 at 19:26
  • trying getting a screenshot right when the click() is issued (use get_screenshot_as_file) – Omid N Dec 14 '18 at 19:33
  • added screenshot. – Brad Dec 14 '18 at 19:53
  • I meant getting the screenshot using selenium and checking if the capture shows if the page is loaded or not :) – Omid N Dec 14 '18 at 20:06
  • I tried doing mydriver.get_screenshot_as_file('/home/myusername/Desktop/output.jpg') but nothing is being produced? – Brad Dec 14 '18 at 20:47
  • I've added the following: `xpaths = { 'pictureButton' : "//*[@id='picture-trigger']" } element = WebDriverWait(mydriver, 20).until( EC.element_to_be_clickable((By.XPATH, xpaths['pictureButton']))); element.click();` but i see the same behavoir, in the UI it looks like web driver is clicking the element but the javascript event isn't kicking off which loads the div. – Brad Dec 14 '18 at 21:24