简体   繁体   中英

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

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM