Skip to content Skip to sidebar Skip to footer

Simulate A Onclick With Selenium Python

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 )

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

Solution 2:

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.

Post a Comment for "Simulate A Onclick With Selenium Python"