We can enter values in an edit box in Selenium with the help of the methods listed below −Using the send_keys method.This method can send any text to an edit box or perform pressing keys with the help of Keys class.Using the Javascript executor.Javascript Document Object Model can work with any of the elements on the page. Javascript works on the client side and performs actions on the web page. Selenium can execute a Javascript script with the help of execute_script() method. We can enter values on any edit box with the help of this method.ExampleCode Implementation with send_keys method.from ... Read More
We can fetch values from a webelement in Selenium with the help of the methods listed below −Using the text method.This will give the inner text of the webelement. It basically gives us the visible text on the screen and its sub element if any. This method will also remove all the forward and backward white spaces.Using the Javascript executor.Javascript Document Object Model can work with any of the elements on the page. Javascript works on the client side and performs actions on the web page. Selenium can execute a Javascript script with the help of execute_script() method. We can ... Read More
We can identify a parent from its children in DOM with the help of xpath. There are situations where we have dynamic attributes for the parent node in html but the child nodes have unique static attributes for identification.This can be achieved with the help of relative xpath along with the parent xpath axe. Method.Syntax driver. find_element_by_xpath("//input[@id='job']/parent::div")ExampleCode Implementation for child to parent traversal.from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke #actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the ... Read More
Xpath is one the locators used in Selenium to identify elements uniquely on a web page. It traverses the DOM to reach the desired element having a particular attribute with/without tagname.The xpath can represented by the ways listed below −//tagname[@attribute='value']//*[@attribute='value']The xpath can be created by the following methods −OR & AND expression.following- sibling expression.parent.child.ancestor.self.descendant.starts-with()ends-with()text()preceding.There are two types of xpath – absolute and relative.Relative xpath – This path begins from any part of the DOM html. It is represented by double slash // and helps to identify elements from any part of the web page and the xpath expression is not ... Read More
Both xpath and css are one the most frequently used locators in Selenium. Though there are other locators like id, name, classname, tagname, and link text and so on, xpath and css are used when there are no unique attributes available to identify the elements.There are some differences between xpath and css listed below −Xpath allows bidirectional flow which means the traversal can be both ways from parent to child and child to parent as well. Css allows only one directional flow which means the traversal is from parent to child only.Xpath is slower in terms of performance and speed. ... Read More
There are multiple ways of building a customized xpath. In case we need to identify nth element we can achieve this by the ways listed below.position() method in xpath.Suppose we have two edit boxes in a page with similar xpath and we want to identify the first element, then we need to add the position()=1.Syntax −driver.find_element_by_xpath("//input[@type='text'][position()=1]")square bracket addition with braces to indicate index.Suppose we need to reach the third row of the table and the customized xpath for that row should be indicated with the help of [3] expression.Syntax −driver.find_element_by_xpath("//table/tbody/tr[2]/td[2]")ExampleCode Implementation with position()from selenium import webdriver #browser exposes an executable ... Read More
The css is one of the important locators in Selenium. A customized css can be developed with the help of attributes like id, classname and by the combination of tagname and html attributes.The ways of creating a css are listed below −Using a class name html attribute.This will select the web element of that particular class represented by (.)classname.Syntax −driver. find_element_by_css_selector(".name")Here name is the value of the attribute class.Using an id html attribute.This will select the web element of that particular id represented by (#) id.Syntax−driver. find_element_by_css_selector("#search")Here search is the value of the attribute id.Using a combination of tagname and ... Read More
We can create a customized xpath with the help of the visible text on the page. This is achieved with the help of text() method in xpath.The text() finds the object with the exact text match on the page.Syntaxdriver.find_element_by_xpath("//input[text()='Selenium']")It will search for elements with visible text 'Selenium' on the page.ExampleCode Implementation with text().from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() # identifying ... Read More
We can identify elements by matching its attributes partially with the help of regular expressions. In xpath, there are multiple methods to achieve this. They are listed below −Using the contains() method. This means the string contains our given text.Syntax −driver.find_element_by_xpath("//input[contains(@name, 'sel')]")It will search the input tag which contains the 'name' attribute containing 'sel' text.Using the starts-with() method. This means the string starts with our given text.Syntax −driver.find_element_by_xpath("//input[starts-with (@name, 'Tut')]")It will search the input tag which contains the 'name' attribute starting with 'Tut' text.Using the ends-with() method. This means the string ends with our given text.Syntaxdriver.find_element_by_xpath("//input[ends-with (@name, 'nium')]")It will search ... Read More
We can identify elements by matching its attributes partially with the help of regular expressions. In css, there are multiple methods to achieve this. They are listed below −Using the wild character *. This means the string contains our given text.Syntax−driver.find_element_by_css_selector("input[name*='sel']")It will search the input tag which contains the 'name' attribute containing 'sel' text.Using the wild character ^. This means the string starts with our given text.Syntax−driver.find_element_by_css_selector("input[name^='Tut']")It will search the input tag which contains the 'name' attribute starting with 'Tut' text.Using the wild character $. This means the string ends with our given text.Syntax−driver.find_element_by_css_selector("input[name$='nium']")It will search the input tag which ... Read More