简体   繁体   中英

Invoking script from Selenium Python

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM