Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to analyze the location elements of Se (5)

Shulou Source: shulou.com Published: 2022-06-01 04:32:07 11月13日 Update

How to carry out Se (5) positioning element analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Package testWd

Import org.openqa.selenium.By

Import org.openqa.selenium.WebDriver

Import org.openqa.selenium.WebElement

Import org.openqa.selenium.firefox.FirefoxDriver

Import org.testng.annotations.Test

Public class findEle {

@ Test

Public void findElement () throws InterruptedException {

System.setProperty ("webdriver.firefox.bin", "D:/Firefox/firefox.exe")

WebDriver dr = new FirefoxDriver ()

Dr.manage (). Window (). Maximize ()

Dr.get ("http://www.baidu.com");"

/ / 1.id

/ * WebElement el1 = dr.findElement (By.id ("kw"))

El1.sendKeys ("English"); * /

/ / 2.name

/ * WebElement el1 = dr.findElement (By.name ("wd"))

El1.sendKeys ("Chinese"); * /

/ / 3.className

/ * WebElement el1 = dr.findElement (By.className ("s_ipt"))

El1.sendKeys ("Spain"); * /

/ / 4.cssSelector

/ * WebElement el1 = dr.findElement (By.cssSelector ("# kw"))

El1.sendKeys ("Taiwan"); * /

/ * WebElement el1 = dr.findElement (By.cssSelector (".s _ ipt"))

El1.sendKeys ("shanghai"); * /

/ * WebElement el1 = dr.findElement (By.cssSelector (".quickdelete-wrap input"))

El1.sendKeys ("beijing"); * /

/ / 5.linktext

/ * WebElement el1 = dr.findElement (By.linkText ("hao123"))

El1.click (); * /

/ / 6.partial linktext

/ * WebElement el1 = dr.findElement (By.partialLinkText ("hao1"))

El1.click (); * /

/ / 7.tagname is not recommended, because if there are multiple tags on the page, only the first tag will be selected.

/ * dr.get ("https://passport.cnblogs.com/user/signin");"

WebElement el1 = dr.findElement (By.tagName ("a"))

El1.click (); * /

/ / using xpath, there are three options:

/ / the first choice is to find id,name first

/ / the second choice, using hierarchical positioning, first locate the parent node, and then locate the child node through the parent node

/ / the third choice is to use document positioning. As long as an element changes or the hierarchy changes, it doesn't work.

/ / 8.xpath, format: / / tag [@ attribute =']

/ * WebElement el1 = dr.findElement (By.xpath ("/ / input [@ id='kw']"))

El1.sendKeys ("qinhuangdao"); * /

/ / xpath,id, tags can be omitted and easy to write, but efficiency may be reduced

/ * WebElement el1 = dr.findElement (By.xpath ("/ / * [@ id='kw']"))

El1.sendKeys ("jinan"); * /

/ / xpath,name

/ * WebElement el1 = dr.findElement (By.xpath ("/ / * [@ name='wd']"))

El1.sendKeys ("tianjin"); * /

/ / hierarchical positioning, non-xpath. Locate the parent node first, and then locate the child node through the parent node.

/ * WebElement el1 = dr.findElement (By.id ("form")) .findElement (By.id ("kw"))

El1.sendKeys ("gansu"); * /

/ * WebElement el1 = dr.findElement (By.id ("form")) .findElement (By.xpath ("span/input"))

El1.sendKeys ("xinjiang"); * /

/ / hierarchical positioning, xpath, first locate the parent node, and then locate the child node through the parent node-pseudo-documented hierarchical positioning

/ * WebElement el1 = dr.findElement (By.xpath ("/ / * [@ id='form'] / span/input"))

El1.sendKeys ("heilongjiang"); * /

/ / xpath, document positioning. Once an element has changed or its hierarchy has changed, it will not work.

WebElement el1 = dr.findElement (By.xpath ("/ html/body/div [2] / div/div/div/div/form/span/input"))

El1.sendKeys ("laiwu")

/ / Thread.sleep (3000)

/ / dr.quit ()

}

}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Positioning nodes levels selections elements positions documents tags bad help analysis clarity three content multiple for this attributes efficiency articles novice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Huawei Apple MariaDB