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

Time:02-14

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related