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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With