Can the By strategy tagName be replaced with cssSelector for the same value?

170 Views Asked by At

I replaced the locator tagName with cssselector without changing the arguments and the code still worked perfectly. The previous script was:

Driver.findElement(By.tagName("*enter tagName*");

Replacement code is:

Driver.findElement(By.cssSelector("*enter tagName*");

The code worked despite the fact that I did not use any cssSelector combination.

How is that possible?

2

There are 2 best solutions below

0
Prophet On

This worked correctly since tag name alone is a correct CSS Selector.
Generally CSS Selector is may look like the following: tagName[attributeName='attributeValue'] where you can omit the attribute name and value and locate the element based on tagName only. So tagName lonely will still be a correct CSS Selector.

0
undetected Selenium On

By.TAG_NAME is always equivalent to By.CSS_SELECTOR


As per the definition of find_element():

elif by == By.TAG_NAME:
    by = By.CSS_SELECTOR
    

Hence the previous line of code:

Driver.findElement(By.tagName("enter tagName");

and the replacement line of code:

Driver.findElement(By.cssSelector("enter tagName");

was equivalent.