我目前正在使用 Walmart's job site我正在尝试使用 Selenium 和 python 登录。到目前为止,我正在使用以下代码:
from selenium import webdriver
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchWindowException
from selenium.common.exceptions import WebDriverException
import argparse
import time
import traceback
import sys
while True:
try:
func_name = driver.find_element_by_id('extension_4').get_attribute('onkeyup')
func_name = func_name[func_name.index(':')+1:]
num = driver.execute_script('return ' + func_name)
#js_functions.append(num)
print repr(num)
except NoSuchElementException:
exc_info = sys.exc_info()
traceback.print_exc()
except NoSuchWindowException: # No Need to Continue if User closes Window
break;
try:
driver.execute_script('return username(arguments[0])', 'ikegwukc@yahoo.com')
except WebDriverException:
exc_info = sys.exc_info()
traceback.print_exc()
time.sleep(2)
当我从这一行打印 num 时:
num = driver.execute_script('return ' + func_name).
它打印用户名和密码。然后我尝试使用以下行更新给定的函数(例如用户名):
driver.execute_script('return username(arguments[0])', 'test@yahoo.com')
它提示没有这样的功能我做错了什么?我已经坚持了几个星期,现在任何指导或解决方案都会真正帮助我。
最佳答案
除非您有特殊原因使用 JS,否则 selenium 可以为您做到这一点:
driver.find_element_by_id('username').send_keys(username_variable)
driver.find_element_by_id('password').send_keys(password_variable)
driver.find_element_by_id('event-submit').click()
您还有其他问题吗?
关于javascript - 从 Javascript 返回 Python Selenium 中的变量并用 selenium 更新它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38004003/