javascript - 从 Javascript 返回 Python Selenium 中的变量并用 selenium 更新它们

标签 javascript python selenium

我目前正在使用 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/

相关文章:

javascript - 对 Angular 和 rails 一起布线感到困惑

python - 在查询中使用 pandas 和命名参数从 mysql 表中读取

user-interface - 如何模拟使用Selenium在html文本输入中按Enter键?

javascript - Puppeteer:获取 innerHTML

javascript - 移至选项卡但不重新加载

javascript - 将commonjs模块与ES6模块混合以导出两个函数

Python:删除重复代码以在 Selenium 中重复操作

javascript - 除了 Karma 之外,Selenium 还涵盖了哪些测试?

javascript - (Javascript) Div 切换和 cookie

python - 音频处理