Of the Many Findelement(S)/By Functions in Selenium, When Would You Use One Over the Other

Of the many findElement(s)/By functions in Selenium, when would you use one over the other?

This question have been asked and answered in numerous forums in different formats. Considering them all if we prioritize the locators the list would be as follows :

  • id: Select element with the specified id attribute.
  • name: Select first element with the specified name attribute.
  • link_text: Select link (anchor tag) element which contains text matching the specified LinkText.
  • partial_link_text: Select link (anchor tag) element which contains text matching the specified PartialLinkText.
  • tag_name: Locate Element using a Tag Name.
  • class_name: Locate Element using a ClassName.
  • css_selector: Select the element using CssSelectors.
  • xpath: Locate an element using an XPath expression.

So the question now is Whats New?

The answer is Selenium have evolved a lot recently. WebDriver is now a W3C Recommendation Candidate. Things within Selenium are changing pretty fast. It's no more only about choosing the locator. We need to use a locator which will :

  • Uniquely identify an element.
  • The performance of the locator must be optimized.

Keeping these two factors in mind, the best strategy would be to Mock the DOM. The W3C Recommendation Candidate does mentions the list of the locators as per the below :

Selenium_Locators

So the verdict is clear and concise.

Selenium test passes on hovering drop-down menu item click but does nothing

To Mouse Hover on Dit is Cloudwise and then to click on Alle Cludwisers you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following locator strategies:

  • Using xpath:

    new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Dit is Cloudwise']")))).build().perform();
    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Dit is Cloudwise']//following::ul[1]/li/a"))).click();

How to locate the email address element with a Facebook page using Selenium

The Email Address field within the webpage contains dynamic elements.

To locate the Email Address field instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using XPATH and the text @:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., '@')]"))).text)
  • Using XPATH and the texts @ & com:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., '@') and contains(., 'com')]"))).text)
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC


Related Topics



Leave a reply



Submit