Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python EXEC show错误:未定义名称_Python_Selenium_Exec - Fatal编程技术网

Python EXEC show错误:未定义名称

Python EXEC show错误:未定义名称,python,selenium,exec,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')) 但我有错误,我

我使用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')
如果要通过
id
xpath
css
进行搜索。。。只需使用适当的值设置
search\u方法

或者:

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')

是否有任何理由使用
exec
?同时让我们知道这是您的实际代码还是简化代码。如果是简化代码-共享您使用的精确代码(如上所述),代码应按预期工作。这里是导入。搜索方法和搜索词来自属性文件
find\u element\u by\u class\u name=keyword
,因此我必须使用EXEC。这是简化的代码,我不知道为什么它不起作用,还有什么我应该注意的吗?我尝试了
elem\u search\u word=driver.find\u element('find\u element\u by\u class\u name','keyword')
,我得到的错误是
'value':value}['value']
不通过
'find\u element\u by\u class\u name'
,使用
“class name”
“id”
“name”
“xpath”
,等等:
驱动程序。查找元素('classname','keyword')
。您还可以导入并使用
驱动程序。查找元素(By.CLASS\u NAME,'keyword')
好了,我将属性更改为
CLASS NAME=keyword
,现在它可以工作了!谢谢!