In python I have:
driver.find_elements(by=By.TAG_NAME, value='a')
How can I change this line to include also b
, c
, d
etc... values?
I tried this but it didn't work:
driver.find_elements(by=By.TAG_NAME, value='a', Value='b')...
In python I have:
driver.find_elements(by=By.TAG_NAME, value='a')
How can I change this line to include also b
, c
, d
etc... values?
I tried this but it didn't work:
driver.find_elements(by=By.TAG_NAME, value='a', Value='b')...
If we take a look at the source code of find_elements
def find_elements(self, by=By.ID, value=None):
"""
Find elements given a By strategy and locator. Prefer the find_elements_by_* methods when
possible.
:Usage:
elements = driver.find_elements(By.CLASS_NAME, 'foo')
:rtype: list of WebElement
it takes only two args, so having this driver.find_elements(by=By.TAG_NAME, value='a', Value='b')
will not work.
However, to address the issue that you've reported here, I do not think TAG_NAME
can be used as a locator strategies
Thus using CSS you could do this:
driver.find_elements(By.CSS_SELECTOR, "tag_name[attributeA = 'valueA'][attributeB = 'valueB']")
Using XPath:
driver.find_elements(By.XPATH, "//tag_name[@attributeA = 'valueA' and @attributeB = 'valueB']")
To answer to your question lets say you want to check multiple HTML
tag using find elements. The best way you could do is CSS_SELECTOR
or XPATH
.
By Using CSS Selector let say you want check div
or span
or input
tag
Your code should be like
driver.find_elements(By.CSS_SELECTOR,"div, span, input")
By XPATH
driver.find_elements(By.XPATH,"//*[self::div|self::span|self::input]")
Hope this example will help.
As the other answerers indicated, By.TAG_NAME
accepts only one aggument. In such cases to select elements with multiple By.TAG_NAME
e.g. <a>
, <b>
, <c>
, etc, you can use either of the following locator strategies:
Using CSS_SELECTOR:
elements = driver.find_elements(By.CSS_SELECTOR, "a, b, c")
Using TAG_NAME through lambda
expression:
elements = driver.find_elements(lambda driver: driver.find_element(By.TAG_NAME, "a") or driver.find_element(By.TAG_NAME, "b") or driver.find_element(By.TAG_NAME, "c"))