Running JavaScript in Selenium Using Python

Running javascript in Selenium using Python

Try browser.execute_script instead of selenium.GetEval.

See this answer for example.

Selenium in Python - Running JavaScript

driver.findElement is Selenium syntax, while inside driver.execute_script you need JavaScript syntax:

driver.execute_script("some javascript code comes here");

To find the right function, look at document DOM object: just like document.getElementById, which you previously used, it has document.getElementsByName() function. Note that unlike ID, that can return 1 element only, there can be many elements with the same name, hence function returns them all as an array. So your statement becomes:

driver.execute_script("document.getElementsByName('estimated-monthly-usage')[0].value='1000'");

Execute Javascript using Selenium in Python

If $ represents jQuery, you would first have to load it into the browser as follows if it has not already been loaded by the current page:

path_to_jquery_js = '/my_scripts/jquery.js' # for example
with open(path_to_jquery_js, 'r') as f:
jquery_js = r.read()
browser.execute_script(jquery_js)

If you do not have jQuery stored locally, then you would have to use something like the requests module from the PyPi repository to fetch it:

import requests

jquery_url = 'https://some_domain/jquery.js' # for example
r = requests.get(jquery_url)
jquery_js = r.text
browser.execute_script(jquery_js

Then you should be able to execute the script as follows:

script = """
var ep_start = $('#episode_page a.active').attr('ep_start');
var ep_end = $('#episode_page a.active').attr('ep_end');
var id = $("input#movie_id").val();
var default_ep = $("input#default_ep").val();
var alias = $("input#alias_anime").val();
loadListEpisode('#episode_page a.active',ep_start,ep_end,id,default_ep,alias);
"""

browser.execute_script(script)

Just make sure loadListEpisode is already defined by other JavaScript that has already been loaded.

This is untested by me for obvious reasons, but give it a shot -- it should work in principle. Let me know how it goes (I am sure you will).

How to click an element with Javascript in Selenium Python

There are basically 4 ways to click in Selenium.

I will use this xpath

//IMG[@NAME='login_button']//parent::A

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//IMG[@NAME='login_button']//parent::A").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//IMG[@NAME='login_button']//parent::A"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//IMG[@NAME='login_button']//parent::A")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//IMG[@NAME='login_button']//parent::A")
ActionChains(driver).move_to_element(button).click().perform()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Running a for loop in JavaScript through Python-Selenium?

To execute the multiline loop Javascript you need to pass the script as an argument to execute_script() method as follows:

driver.execute_script("""
for (let i = 0; i < Values.length; i++) {
if (Values[i].getAttribute('automation-id') === 'contact-wealth-manager') {
ele = Values[i]
}
}
""")

selenium how to run a javascript function

In Python, there are a few components and methods to executing Javascript:

  1. Find the element you want to execute script on
  2. Pass in the element as a Javascript argument

This translates to:

# 1. find element
element = driver.find_element_by_xpath("//div[@class='accept']")

# 2. execute script
driver.execute_script("arguments[0].click();", element)

The Javascript function is reading in element as a parameter and performing .click() on the element. You do not have to pass in a WebElement, and you can use something like document.getElementById() in the Javascript function itself:

driver.execute_script("document.getElementById('id').click();")

Given the HTML for the div element you have provided, I don't think you actually need to use Javascript at all though. You should just be able to use:

driver.find_element_by_xpath("//div[@class='accept']").click()


Related Topics



Leave a reply



Submit