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
Python/selenium-定位元素';由';-使用变量指定定位器策略?_Python_Selenium - Fatal编程技术网

Python/selenium-定位元素';由';-使用变量指定定位器策略?

Python/selenium-定位元素';由';-使用变量指定定位器策略?,python,selenium,Python,Selenium,创建了selenium浏览器并检索了一个网页后,这一切都很好: ... if selenium_browser.find_element(By.ID, 'id_name'): print "found" ... 给定这样一个元组: tup = ('ID', 'id_name') 我希望能够找到如下元素: if selenium_browser.find_element(By.tup[0], tup[1]): 但是我得到了这个错误 AttributeError: type objec

创建了selenium浏览器并检索了一个网页后,这一切都很好:

...
if selenium_browser.find_element(By.ID, 'id_name'):
    print "found"
...
给定这样一个元组:

tup = ('ID', 'id_name')
我希望能够找到如下元素:

if selenium_browser.find_element(By.tup[0], tup[1]):
但是我得到了这个错误

AttributeError: type object 'By' has no attribute 'tup'
我如何做到这一点而不必写:

if tup[0] == 'ID':
    selenium_browser.find_element(By.ID, tup[1])
    ...
elif tup[0] == 'CLASS_NAME':
    selenium_browser.find_element(By.CLASS_NAME, tup[1])
    ...
elif tup[0] == 'LINK_TEXT':
    etc etc

如果要直接提供一个元组到
查找元素
,请在前面添加一个
*

locator = (By.ID, 'id')
element = driver.find_element(*locator)
或者使用作为字符串提供的方法:

locator = ('ID', 'id_name')
driver.find_element(getattr(By, locator[0]), locator[1])

如果要直接提供一个元组到
查找\u元素
,请在前面添加一个
*

locator = (By.ID, 'id')
element = driver.find_element(*locator)
或者使用作为字符串提供的方法:

locator = ('ID', 'id_name')
driver.find_element(getattr(By, locator[0]), locator[1])
你的语法不正确

文档说应该这样使用:
find\u元素(by='id',value=None)

所以不是

if selenium_browser.find_element(By.tup[0], tup[1]):
你应该这样做

if selenium_browser.find_element(tup[0], tup[1]):
#or
if selenium_browser.find_element(by=tup[0], value=tup[1]):
您可能需要也可能不需要将
元素IE
tup[0]进行小写。lower()
您的语法已关闭

文档说应该这样使用:
find\u元素(by='id',value=None)

所以不是

if selenium_browser.find_element(By.tup[0], tup[1]):
你应该这样做

if selenium_browser.find_element(tup[0], tup[1]):
#or
if selenium_browser.find_element(by=tup[0], value=tup[1]):
您可能需要也可能不需要将
元素IE
tup[0]小写。小写()