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

8
  • Hi.. I'm not sure I understand, the href action is a javascript no-op. What are you expecting to observe or have happen? Commented Dec 14, 2018 at 18:58
  • CSS selector and xpath are two different ways of addressing an element. which one are you trying to use? Commented Dec 14, 2018 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. Commented Dec 14, 2018 at 19:08
  • @cody When I click the element interactively/manually a new window (i think its actually a <div> or <span> 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. Commented Dec 14, 2018 at 19:09
  • To clarify it is a div (photobuttonholder). Commented Dec 14, 2018 at 19:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

8 Comments

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.
Is there a delay before making the click action? Maybe the page is still loading while the click is being issued.
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.
trying getting a screenshot right when the click() is issued (use get_screenshot_as_file)
added screenshot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.