本文整理汇总了Python中selenium.webdriver.Firefox.execute_script方法的典型用法代码示例。如果您正苦于以下问题:Python Firefox.execute_script方法的具体用法?Python Firefox.execute_script怎么用?Python Firefox.execute_script使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver.Firefox
的用法示例。
在下文中一共展示了Firefox.execute_script方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_url
# 需要导入模块: from selenium.webdriver import Firefox [as 别名]
# 或者: from selenium.webdriver.Firefox import execute_script [as 别名]
def read_url(url):
driver = Firefox(options=options)
driver.maximize_window()
driver.get(url)
time.sleep(4)
height = driver.execute_script("return document.body.scrollHeight")
print(height)
position = 0
while position < height:
driver.execute_script(f"window.scrollTo(0, {position});")
delta = random.randint(50, 500)
position += delta
duration = delta // 20
# print(height, position, delta, duration)
time.sleep(duration)
driver.close()
示例2: load_cookie
# 需要导入模块: from selenium.webdriver import Firefox [as 别名]
# 或者: from selenium.webdriver.Firefox import execute_script [as 别名]
def load_cookie(self, session_id):
sessions = json.load(open( self.sessions_file ))
cookie_path = sessions[str(session_id)]["session_path"]
url = sessions[str(session_id)]["web_url"]
# Setting useragent to the same one the session saved with
useragent = sessions[str(session_id)]["useragent"]
profile = FirefoxProfile()
profile.set_preference("general.useragent.override", useragent )
cookies = pickle.load(open(cookie_path, "rb"))
try:
browser = Firefox(profile)
except:
error("Couldn't open browser to view session!")
return
browser.get(url)
browser.delete_all_cookies()
browser.execute_script("window.localStorage.clear()") # clear the current localStorage
for cookie in cookies:
browser.add_cookie(cookie)
status(f"Session {session_id} loaded")
browser.refresh()
self.browsers.append(browser)
示例3: login
# 需要导入模块: from selenium.webdriver import Firefox [as 别名]
# 或者: from selenium.webdriver.Firefox import execute_script [as 别名]
def login(username, password):
"""Login into website, return cookies, api and sso token using geckodriver/firefox headless"""
display = Display(visible=0, size=(800, 600))
display.start()
# options = Options()
# options.add_argument('-headless')
# driver = Firefox(executable_path='/usr/local/bin/geckodriver', firefox_options=options)
driver = Firefox()
wait = WebDriverWait(driver, timeout=10)
driver.get(url)
time.sleep(10)
username_field = driver.find_element_by_name("emailOrPcrNumber")
# There are multiple entries with the name pin, use the xpath instead even though it is more error prone
# password_field = driver.find_element_by_name("pin")
password_field = driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div[1]/div[2]/form/div/div[1]/div[2]/input')
username_field.clear()
username_field.send_keys(username)
password_field.clear()
password_field.send_keys(password)
time.sleep(2)
driver.find_element_by_id("tpiSubmitButton").click()
time.sleep(3)
cookies = driver.get_cookies()
for cookie in cookies:
if cookie['name'] == 'X-IHG-SSO-TOKEN':
sso_token = cookie['value']
api_key = driver.execute_script('return AppConfig.featureToggle.apiKey')
driver.get('https://apis.ihg.com')
cookies.extend(driver.get_cookies())
driver.quit()
display.stop()
return api_key, sso_token, cookies
示例4: Hertz
# 需要导入模块: from selenium.webdriver import Firefox [as 别名]
# 或者: from selenium.webdriver.Firefox import execute_script [as 别名]
class Hertz():
def __init__( self ):
self.driver = Firefox()
#self.driver.get( 'https://www.hertzequip.com/herc/rental-equipment/aerial-equipment+manlifts+articulating-boom-lift' )
self.driver.implicitly_wait( 15 )
def perform_search( self, zip_code ):
self.driver.get( 'https://www.hertzequip.com/herc/rental-equipment/aerial-equipment+manlifts+articulating-boom-lift' )
# wait for javascript to load the page before we begin
self.driver.find_element_by_xpath( '//span[text()="View Rates"]' )
# selenium commands can be very unreliable
# since Jquery is already loaded, I'll use that
self.driver.execute_script( """
$('span:contains("View Rates")')[0].click()
$('input[id="postalCode"]').val('""" + zip_code + """')
$('a[class="button_four button_four_skin"]')[0].click()
""" )
# wait a few seconds for page element to disappear
sleep( 3 )
# wait until javascript has loaded the page again
self.driver.find_element_by_xpath( '//span[text()="View Rates"]' )
def get_rates( self ):
rates_info_json = self.driver.execute_script( """
var xmlhttp = new XMLHttpRequest()
var url = "https://www.hertzequip.com/herc/rest/rentals/getBuilderEquipmentDetailsItemInfo?catalogType=" + String( catalogType ) + "&categoryId=" + categoryId + "&subcategory1Id=" + subcategory1Id + "&subcategory2Id=" + subcategory2Id + "&_=" + String( new Date().getTime() )
xmlhttp.open( "GET", url, false )
xmlhttp.send()
return xmlhttp.responseText
""" )
return rates_info_json
def get_store_info( self ):
store_info_json = self.driver.execute_script( """
var xmlhttp = new XMLHttpRequest()
var url = "https://www.hertzequip.com/herc/rest/rentals/getBuilderJobLocationsItemInfo?_=" + String( new Date().getTime() )
xmlhttp.open( "GET", url, false )
xmlhttp.send()
return xmlhttp.responseText
""" )
return store_info_json
def perform_search_get_data( self, zip_code ):
self.perform_search( zip_code )
location_data = json.loads( self.get_store_info() )
equipment_rates_data = json.loads( self.get_rates() )
specific_data = OrderedDict()
specific_data['branch_zip_code'] = location_data['data']['jobLocation']['branchLocation']['zip']
specific_data['branch_id'] = location_data['data']['jobLocation']['branchLocation']['branch']
specific_data['equipment_list'] = []
for equipment in equipment_rates_data['data']['equipmentInfo']['m_list']:
equipment_data = OrderedDict()
equipment_data['specs'] = [ equipment['spec1'], equipment['spec2'] ]
equipment_data['makes & models'] = equipment['makesModels']['m_list']
equipment_data['rates'] = equipment['ratesInfo']
specific_data['equipment_list'].append( equipment_data )
return {
'location_data' : location_data,
'equipment_rates_data' : equipment_rates_data,
'specific_data' : specific_data
}
示例5: test_we_can_switch_context_to_chrome
# 需要导入模块: from selenium.webdriver import Firefox [as 别名]
# 或者: from selenium.webdriver.Firefox import execute_script [as 别名]
def test_we_can_switch_context_to_chrome(self, capabilities):
driver = Firefox(capabilities=capabilities)
driver.set_context('chrome')
assert 1 == driver.execute_script("var c = Components.classes; return 1;")