2

I'm working with a webpage that has a number of javascript buttons that are written like this:

<a href="javascript:;" onclick="addtocart('888%20B0007VBRUE%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20',$(this),0)" class="btn btn-lblue pull-right">

Selenium can execute javascript, but I'm not quite sure how to format the javascript query to trigger the individual buy buttons. I tried:

driver.execute_script("document.getElementById("test").onclick = "addtocart('888%20B0007VBRUE%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20',$(this),0)").click()

but this gives me the error Message: document.getElementById(...) is null. How can I change what I've tried to get it to work?

2
  • I thought using fake links went out with browser sniffing. What is the point of href="javascript:;" when there are 3 classes already do to styling? Commented May 10, 2016 at 4:51
  • Didn't write the website in question, so I'm not really sure why they did it that way. Commented May 10, 2016 at 4:52

1 Answer 1

2

This is because you have not added an id of test to your <a> tag. Also it is generally not advisable to lookup an element based on the value of its onclick handler.

Fixed code:

<a href="javascript:;" id="test" onclick="addtocart('888%20B0007VBRUE%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20',$(this),0)" class="btn btn-lblue pull-right">

In addition, you need to switch one of your " for ' in driver.execute script, because each " terminates the preceding string.

Example:

driver.execute_script('document.getElementById("test"),$(this),0)').click()
Sign up to request clarification or add additional context in comments.

Comments

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.