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 :
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
Staleelementreferenceexception on Python Selenium
How to Set Your Pythonpath in an Already-Created Virtualenv
Python - Is Time.Sleep(N) Cpu Intensive
Converting Epoch Time With Milliseconds to Datetime
Webdriverwait Not Working as Expected
How to Keep Keys/Values in Same Order as Declared
Why Do People Write #!/Usr/Bin/Env Python on the First Line of a Python Script
Why Does Concatenation of Dataframes Get Exponentially Slower
Selenium.Common.Exceptions.Invalidselectorexception With "Span:Contains('String')"
Run Python Script At Startup in Ubuntu
Yes' Reporting Error With Subprocess Communicate()
Integer Division by Negative Number
How to Iterate Over Rows in a Dataframe in Pandas
Importing Files from Different Folder