Python EXEC 显示错误 : name is not defined

标签 python selenium exec

我使用 exec 和 selenium 来运行如下,

from selenium import webdriver
search_method = 'find_element_by_class_name'
search_word = 'keyword'
exec("elem_search_word = driver." + search_method + "('" + search_word + "')")
elem_search_word.send_keys('python'))

但是我收到错误,我该怎么办?

elem_search_word.send_keys(str(args.search))
NameError: name 'elem_search_word' is not defined

最佳答案

我无法重现您的问题,但恕我直言,在这种情况下您应该避免使用 exec() 。请尝试以下操作:

search_method = 'class name'
search_word = 'keyword'
elem_search_word = driver.find_element(search_method, search_word)
elem_search_word.send_keys('python')

如果您想通过 idxpathcss 进行搜索...只需将 search_method 设置为适当的即可值

或者:

from selenium.webdriver.common.by import By

search_method = By.CLASS_NAME
search_word = 'keyword'
elem_search_word = driver.find_element(search_method, search_word)
elem_search_word.send_keys('python')

关于Python EXEC 显示错误 : name is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52146051/

相关文章:

python - 属性错误 : 'Series' object has no attribute 'to_coo'

python - 每次请求后都应该关闭数据库连接吗?

python - 在 virtualenv 中找不到 Atom.core

php - FFMPEG 和 PHP 在 Mac 上不起作用

java - 通过 exec 或 shell_exec 执行 java 会出错

java - 循环中的 Runtime.getRuntime().exec()

Python从字符串中解析json

java - Selenium 服务器在被 SIGTERM 杀死后不绑定(bind)到套接字

java - 如何将 appium 服务器日志从 eclipse 控制台跳过到 txt 文件

RSelenium,Chrome,如何设置下载目录,文件下载错误