3

I am pretty new to selenium and I am trying to figure out how to simulate a onclick

this is what I see in the source code when I inspect the html source

<a href="#" onclick="document.getElementById('pN').selectedIndex = 0;document.getElementById('optionList').submit();return false"> <img src="images/ListingOptionSearch.jpg" onmouseover="this.src='images/ListingOptionSearchHover.jpg'" onmouseout="this.src='images/ListingOptionSearch.jpg'"> </a>

I tried :

driver.find_element_by_css_selector("a[onlick*=document.getElementById('pN')
.selectedIndex]").click()

but I get a InvalidSelectorException

any idea?

thanks!

Steven G
  • 16,244
  • 8
  • 53
  • 77

2 Answers2

6

You can use

from selenium import webdriver
browser = webdriver.Chrome(somepath) # You should know what this does.
browser.execute_script("document.getElementById('pN').selectedIndex = 0;document.getElementById('optionList').submit();return false")

Meaning you can execute Javascript code just by using .execute_script , Awesome, right?

An invalid InvalidSelectorException is an expecting raised when there's no element or from experience, there could be an iframe and you'll have to use .switch_to.frame to be able to interact with it. Also, I like using XPath (most reliable always), it takes a little bit time getting used to, but with an hour or two of practising you can get it.

JeffC has a good point, the structure of the HTML, JS can always change. You can use the find_element_by_xpath(xpath).click() but there are also more dynamic ways to predict wether the structure is going to change, using something like find_element_by_nameor other that are available:

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
innicoder
  • 2,612
  • 3
  • 14
  • 29
  • You don't really want to do this. This is calling the JS that happens to be on the A tag right now. If the `onclick` ever changes, you won't be doing the same as clicking the A tag any more. Also, many (most?) people are trying to script user scenarios and no user is going to execute JS, They are going to click the link (which then triggers the onclick behavior). – JeffC Apr 21 '17 at 02:48
  • Good point JeffC, this is one way of doing things. You'll mostly have to make changes/tweaks when the sites structure changes (no matter HTML/js). I've added some code and explanations. I found from experience that the website mostly doesn't change things like this tho, scrapers can last 1-10 years without the site changing. You should also add casual try, except blocks to inform you whenever this does occur. – innicoder Apr 21 '17 at 20:25
1

Did you google the exception? It means that your selector is not valid. CSS selectors like the one you are trying to use are in the form tag[attribute='value']. You don't have the value surrounded by quotes... which is not going to be possible in this specific case because your value already contains single quotes.

Because the A tag encloses the IMG tag, you can click on the IMG tag and get the same effect. A CSS selector like the below should work.

img[src='images/ListingOptionSearch.jpg']

There are other selectors that would probably work but with a link to the page, etc. I would be just guessing as to whether they would be unique.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Hey Jeff, thanks for color, so I would do `driver.find_element_by_css_selector("img[src='images/ListingOptionSearch.jpg']").click()` ? – Steven G Apr 21 '17 at 14:06
  • 1
    Yes... that should work since the image is inside the A tag. – JeffC Apr 21 '17 at 14:13