Finding an element in Selenium

0 votes

Can someone tell me, what will be the difference between

find_element_by_xpath(my_xpath) 

and 

find_element(By.XPATH, my_xpath) 

if I use them in my code below:

from selenium import webdriver
from selenium.webdriver.common.by import By

source = "https://www.edureka.co"
my_xpath = '/html/body/div[4]/div[2]/div[2]/div/div[1]/img'
driver = webdriver.Firefox()
driver.get(source)
x = driver.find_element_by_xpath(my_xpath)
y = driver.find_element(By.XPATH, my_xpath)
driver.quit()
Mar 27, 2018 in Selenium by code_ninja
• 6,220 points
132 views

2 answers to this question.

0 votes

Basically, they are not too different. The difference is in managing your objects: find_element() gives more flexibility, as you can create a 

locator = By.XPATH, my_xpath 

somewhere and use it as find_element(*locator) in, for example, your tests. You can simply update locator 

locator = By.CSS, my_css 

and there will be no need in updating your test code as find_element(*locator) is still valid.

answered Mar 27, 2018 by nsv999
• 5,110 points
0 votes

Hi, ​both find_element_by_xpath(xpath) and find_element(By.XPath, xpath) serve the same purpose, but when you use find_element (By.XPATH, xpath) you need to create the xpath instance whereas in find_element_by_xpath() by id instance is created along with the find elements. Its better to go with find_element(by. xpath) as we have the ability to modify the instance at any time.

answered Aug 23 by Abha
• 27,180 points

Related Questions In Selenium

0 votes
1 answer

How to use JavaScript in selenium to click an Element?

Executing a click via JavaScript has some ...READ MORE

answered Apr 13, 2018 in Selenium by Shubham
• 13,300 points
14,364 views
+4 votes
3 answers

Verifying whether an element present or visible in selenium Webdriver

Below code will help you: To check Element ...READ MORE

answered Apr 17, 2018 in Selenium by king_kenny
• 3,650 points
17,214 views
0 votes
1 answer

Wait til an element appears in Selenium?

You should call ignoring with exception to ignore till ...READ MORE

answered May 24, 2018 in Selenium by Meci Matt
• 9,420 points
38 views
0 votes
1 answer

Verify that an element exist in Selenium 2 or not

If you are testing using JUnit and ...READ MORE

answered Aug 23, 2018 in Selenium by Meci Matt
• 9,420 points
220 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
934 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,114 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
856 views
0 votes
2 answers

Unable to Click on an Element in Selenium (Python) even after finding it.

Here, I give you working script which ...READ MORE

answered Sep 19, 2018 in Selenium by Priyaj
• 56,900 points
8,704 views
0 votes
1 answer

Finding parent element in Protractor

To get the parent element, you can ...READ MORE

answered Mar 26, 2018 in Selenium by nsv999
• 5,110 points
1,051 views