Reputation: 23
Having created a selenium browser and retrieved a web page, this works fine:
...
if selenium_browser.find_element(By.ID, 'id_name'):
print "found"
...
given a tuple like this:
tup = ('ID', 'id_name')
I'd like to be able to locate elements like this:
if selenium_browser.find_element(By.tup[0], tup[1]):
but I get this error
AttributeError: type object 'By' has no attribute 'tup'
How can I do this without having to write:
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
http://selenium-python.readthedocs.io/api.html?highlight=#module-selenium.webdriver.common.by
Upvotes: 1
Views: 2352
Reputation: 42538
If you want to directly provide a tuple to find_element
, add a *
in front:
locator = (By.ID, 'id')
element = driver.find_element(*locator)
Or with the method provided as a string:
locator = ('ID', 'id_name')
driver.find_element(getattr(By, locator[0]), locator[1])
Upvotes: 3
Reputation: 41149
Your syntax is off.
The docs say this should be used like such: find_element(by='id', value=None)
So instead of
if selenium_browser.find_element(By.tup[0], tup[1]):
You should do
if selenium_browser.find_element(tup[0], tup[1]):
#or
if selenium_browser.find_element(by=tup[0], value=tup[1]):
You may or may not need to lowercase the by
element IE tup[0].lower()
Upvotes: 1