質問

I am brand new to selenium and am having trouble figuring out how to use Python and Selenium web driver to automate the following statement. This is a clickable text element "My Button Label".

<a class="class_a" href="javascript:f_func(46)" id="func46" ondrag="window.event.returnValue=false" onmousedown="return false;">My Button Label </a>

I've tried this:

driver.execute_script("f_func()", 46)

and this

driver.execute_script("f_func(46)")

but end up with

selenium.common.exceptions.WebDriverException: Message: u'data[i] is undefined' ; 

which points to a var deep inside js.

What is proper way to simulate clicking on that element and invoking the script, passing in 46 as argument?

On edit: I forgot to add the first thing I tried

driver.find_element_by_xx().click()

Also failed. The ActionChain Api per below is the only thing that worked for me.

役に立ちましたか?

解決

To invoke script, have you tried sending your argument?

driver.execute_script("f_func(arguments[0])", 46)

But what's wrong with clicking in the first place? If it doesn't work for you, please try use ActionChains.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

# your code here

# then
ActionChains(driver).click(driver.find_element_by_id('func46')).perform()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top