This article describes the by tagname method in the Findelement interface. First, you need to know what tagname is. Before we used the F12 developer tool to view the page elements, we found that each line of code had <input>, <body>, <div>, <button>, and so on, and these tags were tagname. But we can all see, like <div>, <button>, <input> these commonly used tags, the frequency is very high in the Web page, if we use tagname to locate elements, we may not be able to locate the element we expected. So it is common to use the Findelements interface to find a set of elements.
For example, I want to make Firefox full-screen, so the first thing I want to do is focus the cursor inside the browser. Focusing on this action can be achieved through tagname. By looking at the page code, we know that a Web page <body> this tag is unique. Then we need to use <body> this tag to complete the focus action. The code shows the following
import java.util.concurrent.TimeUnit; import Org.openqa.selenium.by;import Org.openqa.selenium.keys;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.firefox.FirefoxDriver; Public classByclassname { Public Static voidMain (string[] args) throws Exception {System.setproperty ("Webdriver.gecko.driver",". \\Tools\\geckodriver.exe"); Webdriver Driver=NewFirefoxdriver (); Driver.manage (). window (). Maximize (); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Driver.Get("http://news.baidu.com"); Driver.findelement (By.tagname ("Body") . SendKeys (KEYS.F11); }}
For example, I used Firefox instead of the previous Google browser. Because I found that press F11, full screen This operation, in Google Chrome is not successful, no error message. So maybe it's Chromedriver.exe's little bug. The solution to this problem, the follow-up if found, will be updated.
Java + Selenium element positioning (3) by TagName