Mike Esteghamatian Mike Esteghamatian - 2 months ago 12
AngularJS Question

How to set values for AngularJS controller variables using python Selenium or JavaScript console?

I have been using python Selenium and AngularJS for couple of mounts and right now I am trying to modify some AngJS variables (of aslider filter from here) and let the page refresh so that I can scrap some data out the page. I use the below code for that matter.

#! /usr/bin/python2.7
import os
import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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 import ActionChains
import datetime
import time
import mysql.connector

chrome_options = webdriver.ChromeOptions()

os.environ["PATH"] += ":/home/mike/software"

minPrice = 0
maxPrice = 1800000

minBedRoom = 0
maxBedRoom = 9

minBathRoom = 0
maxBathRoom = 8

minSoldDate = 0
maxSoldDate = 39

os.environ["PATH"] += ":/usr/local/bin/"
driver = webdriver.Chrome()
# en = driver.find_element_by_xpath("//span[6]")

element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "(//slider)[1]"))

print ''

ens = driver.find_elements_by_xpath('(//slider)')
print 'ens', ens
en = ens[0]

# set min price
driver.execute_script( '''angular.element(arguments[0]).scope().filters.sold_price_low = {0}; angular.element(document.body).injector().get('$rootScope').$apply();'''
.format(minPrice), ens[0] )

For some reason it doesn't apply the change. Please let me know what I am missing.

Your help is much appreciated.

Thank you,


You forgot to call the ng-change function according to your page (you need call filterPeople()). do it like below:

driver.execute_script( '''angular.element(arguments[0]).scope().filters.sold_price_low = {0}; angular.element(arguments[0]).scope().filterPeople(); angular.element(document.body).injector().get('$rootScope').$apply();'''
    .format(minPrice), ens[0] )