• <tfoot id='QY0pD'></tfoot>
        <i id='QY0pD'><tr id='QY0pD'><dt id='QY0pD'><q id='QY0pD'><span id='QY0pD'><b id='QY0pD'><form id='QY0pD'><ins id='QY0pD'></ins><ul id='QY0pD'></ul><sub id='QY0pD'></sub></form><legend id='QY0pD'></legend><bdo id='QY0pD'><pre id='QY0pD'><center id='QY0pD'></center></pre></bdo></b><th id='QY0pD'></th></span></q></dt></tr></i><div id='QY0pD'><tfoot id='QY0pD'></tfoot><dl id='QY0pD'><fieldset id='QY0pD'></fieldset></dl></div>

        <legend id='QY0pD'><style id='QY0pD'><dir id='QY0pD'><q id='QY0pD'></q></dir></style></legend>

        <small id='QY0pD'></small><noframes id='QY0pD'>

        • <bdo id='QY0pD'></bdo><ul id='QY0pD'></ul>

        如何通过除“类"之外的属性直接找到 WebEle

        时间:2023-09-28

        1. <i id='eEjOZ'><tr id='eEjOZ'><dt id='eEjOZ'><q id='eEjOZ'><span id='eEjOZ'><b id='eEjOZ'><form id='eEjOZ'><ins id='eEjOZ'></ins><ul id='eEjOZ'></ul><sub id='eEjOZ'></sub></form><legend id='eEjOZ'></legend><bdo id='eEjOZ'><pre id='eEjOZ'><center id='eEjOZ'></center></pre></bdo></b><th id='eEjOZ'></th></span></q></dt></tr></i><div id='eEjOZ'><tfoot id='eEjOZ'></tfoot><dl id='eEjOZ'><fieldset id='eEjOZ'></fieldset></dl></div>

              • <legend id='eEjOZ'><style id='eEjOZ'><dir id='eEjOZ'><q id='eEjOZ'></q></dir></style></legend><tfoot id='eEjOZ'></tfoot>

                <small id='eEjOZ'></small><noframes id='eEjOZ'>

                  <tbody id='eEjOZ'></tbody>

                  <bdo id='eEjOZ'></bdo><ul id='eEjOZ'></ul>
                  本文介绍了如何通过除“类"之外的属性直接找到 WebElements和“名称"(例如“标题")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我是 Java 和 Selenium 的新手,所以如果我的问题听起来有点初级,我提前道歉.

                  我使用:

                  driverChrome.findElements(By.className("blabla"));

                  查找以blabla"作为其类名的元素,例如:

                  <span class="blabla" title="标题">...</span>

                  现在,如果我想通过其他属性查找所有元素怎么办?类似:

                  driverChrome.findElements(By.titleValue("the title"));

                  这是我目前用于执行此任务的代码:

                  列表spans = driverChrome.findElements(By.tagName("span"));for (WebElement we : spans) {if (we.getAttribute("title") != null) {if (we.getAttribute("title").equals("the title")) {...休息;}}}

                  但它并不快速且易于使用.

                  解决方案

                  XPath

                  归档元素有很多方法

                  1 绝对路径

                  <html><身体>

                  <表格><输入 id="demo"/></表格></div></身体><html>

                  获取输入"标签

                  xpath="/html/body/div/form/input"

                  2 相对路径

                  <html><身体>

                  <表格><输入 id="demo"/></表格></div></身体><html>

                  获取输入"标签

                  xpath="//输入"

                  3 索引

                  <html><身体>

                  <表格><输入id="demo1"/><输入id="demo2"></表格></div></身体><html>

                  获取输入'demo2'

                  xpath="//输入[1]"

                  4 任意单个属性

                  <html><身体>

                  <表格><输入id="demo1"/><输入 id="demo2" foo="bar"></表格></div></身体><html>

                  获取输入'demo2'

                  xpath="//input[@id='demo2']" (相当于By.id)

                  或者

                  xpath="//input[@foo='bar']"

                  5 任意多个属性

                  <html><身体>

                  <表格><输入id="1" type="提交"/><输入 id="2" foo="bar"/><input id="3" type="submit" foo="bar"/></表格></div></身体><html>

                  获取第三个输入

                  xpath="//input[@type='submit'][@foo='bar']"

                  或者

                  xpath="//input[@type='submit' and @foo='bar']"

                  如果在这里使用 xpath="//input[@type='submit' 或 @foo='bar']" 你会得到一个数组.您可以通过 driver.findElements(By.xpath(xpath)) (java) 获取列表.否则你会得到第一个元素(如果你只使用 driver.findElement).因为所有 3 个输入元素都满足您的条件或",它给了您第一个.

                  6 包含属性

                  <html><身体>

                  <表格><输入id="1" type="提交"/><input id="2" foo="bar" daddy="dog"/><input id="3" type="submit" foo="bar"/></表格></div></身体><html>

                  获取第二个输入

                  xpath="//input[@daddy]"

                  因为只有第二个有属性'daddy'

                  7 内搜索

                   <html><身体>

                  <表格><input id="input1" daddy="dog"/><input id="input2" daddy="pig"/></表格></div>

                  <表格><input id="input3" daddy="dog"/><input id="input4" daddy="apple"/></表格></div></身体><html>

                  获取第二个div

                  xpath="//div[.//input[@daddy='dog'] 和 .//input[@daddy='apple']]"

                  总的来说,这些都是我现在可以解决的.希望对您有所帮助.

                  I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.

                  I use:

                  driverChrome.findElements(By.className("blabla"));
                  

                  to find elements which have "blabla" as their className, for example:

                  <span class="blabla" title="the title">...</span>
                  

                  Now, what if I want to find all elements by their other attributes? something like:

                  driverChrome.findElements(By.titleValue("the title"));
                  

                  This is the code that I am currently using to do this task:

                  List<WebElement> spans = driverChrome.findElements(By.tagName("span"));
                  
                  for (WebElement we : spans) {
                  
                      if (we.getAttribute("title") != null) {
                              if (we.getAttribute("title").equals("the title")) {
                                      ...
                                      break;
                              }
                      }
                  
                  }
                  

                  but it is not fast and easy to use.

                  解决方案

                  There are many methods while archiving an element by XPath

                  1 Absolutely path

                  <html>
                    <body>
                       <div>
                         <form>
                            <input id="demo"/>
                         </form>
                       </div>
                     </body>
                   <html>
                  

                  To get the 'input' tag

                  xpath="/html/body/div/form/input"
                  

                  2 Relative path

                  <html>
                    <body>
                       <div>
                         <form>
                            <input id="demo"/>
                         </form>
                       </div>
                     </body>
                   <html>
                  

                  To get the 'input' tag

                  xpath="//input"  
                  

                  3 Index

                  <html>
                    <body>
                       <div>
                         <form>
                            <input id="demo1"/>
                            <input id="demo2"> 
                         </form>
                       </div>
                     </body>
                   <html>
                  

                  To get the input 'demo2'

                  xpath="//input[1]"

                  4 Arbitrary single attribute

                  <html>
                    <body>
                       <div>
                         <form>
                            <input id="demo1"/>
                            <input id="demo2" foo="bar"> 
                         </form>
                       </div>
                     </body>
                   <html>
                  

                  To get input 'demo2'

                  xpath="//input[@id='demo2']" (equivalent to By.id)
                  

                  Or

                  xpath="//input[@foo='bar']"
                  

                  5 Arbitrary multiple attributes

                  <html>
                      <body>
                       <div>
                         <form>
                            <input id="1" type="submit" />
                            <input id="2" foo="bar"/>
                            <input id="3" type="submit" foo="bar"/> 
                         </form>
                       </div>
                     </body>
                   <html>
                  

                  To get 3rd input

                  xpath="//input[@type='submit'][@foo='bar']"
                  

                  Or

                  xpath="//input[@type='submit' and @foo='bar']"
                  

                  If use xpath="//input[@type='submit' or @foo='bar']" here you'll get an array. You can get the List by driver.findElements(By.xpath(xpath)) (java). Otherwise you'll get the first element(If you just use driver.findElement). Because all of the 3 input elements meet your condition 'or' and it gives you the first one.

                  6 Contains attribute

                  <html>
                      <body>
                       <div>
                         <form>
                            <input id="1" type="submit" />
                            <input id="2" foo="bar" daddy="dog"/>
                            <input id="3" type="submit" foo="bar"/> 
                         </form>
                       </div>
                     </body>
                   <html>
                  

                  To get the second input

                  xpath="//input[@daddy]"
                  

                  Because only the second one has attribute 'daddy'

                  7 Inner searching

                   <html>
                      <body>
                       <div>
                         <form>
                            <input id="input1" daddy="dog" />
                            <input id="input2" daddy="pig"/>
                         </form>
                       </div>
                       <div>
                         <form>
                            <input id="input3" daddy="dog" />
                            <input id="input4" daddy="apple"/>
                         </form>
                       </div>
                     </body>
                   <html>
                  

                  To get the second div

                  xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
                  

                  Overall those are all I can work out for now. Hope it helps.

                  这篇关于如何通过除“类"之外的属性直接找到 WebElements和“名称"(例如“标题")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:ChromeDriver:自定义 Chrome 可执行文件路径 下一篇:Selenium sendkeys 使用 Chrome 驱动程序丢弃字符

                  相关文章

                  最新文章

                  <small id='9Ffx6'></small><noframes id='9Ffx6'>

                  <tfoot id='9Ffx6'></tfoot>
                  <i id='9Ffx6'><tr id='9Ffx6'><dt id='9Ffx6'><q id='9Ffx6'><span id='9Ffx6'><b id='9Ffx6'><form id='9Ffx6'><ins id='9Ffx6'></ins><ul id='9Ffx6'></ul><sub id='9Ffx6'></sub></form><legend id='9Ffx6'></legend><bdo id='9Ffx6'><pre id='9Ffx6'><center id='9Ffx6'></center></pre></bdo></b><th id='9Ffx6'></th></span></q></dt></tr></i><div id='9Ffx6'><tfoot id='9Ffx6'></tfoot><dl id='9Ffx6'><fieldset id='9Ffx6'></fieldset></dl></div>

                      <bdo id='9Ffx6'></bdo><ul id='9Ffx6'></ul>

                    <legend id='9Ffx6'><style id='9Ffx6'><dir id='9Ffx6'><q id='9Ffx6'></q></dir></style></legend>