如何在 Selenium 中通过 XPath 存储和回显当前输入字段的所有 ID?
How to store and echo all ID's of present input fields by XPath in Selenium?
我正在学习用于自动化测试的 Selenium,我正在经历并尝试为网站上的基本帐户注册建立一些测试步骤。
该过程将涉及获取表单中输入字段的所有 ID,然后将这些 ID 保存到一个变量并回显所有这些 ID。
目前我的 XPath 看起来像:
//*[contains(concat(' ', normalize-space(@class), ' '), ' textField')]/descendant::input
其中,在 Firebug 中突出显示所有输入字段。
现在我的问题是,为了 verification/debugging 目的,我将如何保存这些输入字段的 ID 并将它们回显到 Selenium 中?
我试着从 How to store the content/value of xpath? 那里得到一个更好的主意,但唯一回显并保存在临时变量中的只是我给它的变量的名称。
(我们称这个变量为 "AllFormInputIDs")
非常感谢任何和所有的帮助,任何关于更高效的 XPath mark-up/code 标记的提示都会很棒!谢谢:)
找到元素后,您可以使用 getAttribute
方法检索元素的附加属性并存储它。
假设我们要在页脚内打印所有以 'Stack' 开头的 href
链接:-
python代码:
for element in driver.find_elements_by_xpath("//*[@id='footer']//../a[contains(.,'Stack')]"):
print(element.get_attribute('href'))
打印:
https://whosebug.com/
https://stackapps.com/
https://meta.stackexchange.com/
http://careers.whosebug.com/
正在打印 Ask Questions
按钮的 ID
print(driver.find_element_by_xpath("//*[text()='Ask Question']").get_attribute('id'))
nav-askquestion
Python 文件的工作示例:GitHubFile
您可以按照以下流程:
- 找出页面中所有的
input
标签元素,存入列表
- 遍历列表,使用
getAttribute()
方法获取属性 id
并将其存储在另一个 Arraylist 中,比如你提到的 AllFormInputIDs
。
- 现在您可以遍历 "AllFormInputIDs" 的列表来执行您想要的任何操作。
下面是上述过程的Java代码片段:
//Opening firefox driver instance, maximizing and assigining an implicit timeout of 20 seconds.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Navigating to the site
driver.get("");
//Fetching all the elements with tag 'input' and putting in a List
List<WebElement> List_Inputs = driver.findElements(By.tagName("input"));
//Creating an ArrayList object
ArrayList<String> AllFormInputIDs = new ArrayList<String>() ;
//Iterating throught the input ids in List_Inputs and fetching the 'id' attribute
//where 'id' value is not an empty string
for(WebElement inputID: List_Inputs){
if(!inputID.getAttribute("id").equals(""))
AllFormInputIDs.add(inputID.getAttribute("id"));
}
//Iterating over AllFormInputIDs where the fetched id's of all Input tags are present
//and printing them
int i=1;
for(String id: AllFormInputIDs){
System.out.println("ID "+(i++)+": "+id);
}
我正在学习用于自动化测试的 Selenium,我正在经历并尝试为网站上的基本帐户注册建立一些测试步骤。
该过程将涉及获取表单中输入字段的所有 ID,然后将这些 ID 保存到一个变量并回显所有这些 ID。
目前我的 XPath 看起来像:
//*[contains(concat(' ', normalize-space(@class), ' '), ' textField')]/descendant::input
其中,在 Firebug 中突出显示所有输入字段。
现在我的问题是,为了 verification/debugging 目的,我将如何保存这些输入字段的 ID 并将它们回显到 Selenium 中?
我试着从 How to store the content/value of xpath? 那里得到一个更好的主意,但唯一回显并保存在临时变量中的只是我给它的变量的名称。
(我们称这个变量为 "AllFormInputIDs")
非常感谢任何和所有的帮助,任何关于更高效的 XPath mark-up/code 标记的提示都会很棒!谢谢:)
找到元素后,您可以使用 getAttribute
方法检索元素的附加属性并存储它。
假设我们要在页脚内打印所有以 'Stack' 开头的 href
链接:-
python代码:
for element in driver.find_elements_by_xpath("//*[@id='footer']//../a[contains(.,'Stack')]"):
print(element.get_attribute('href'))
打印:
https://whosebug.com/
https://stackapps.com/
https://meta.stackexchange.com/
http://careers.whosebug.com/
正在打印 Ask Questions
按钮的 ID
print(driver.find_element_by_xpath("//*[text()='Ask Question']").get_attribute('id'))
nav-askquestion
Python 文件的工作示例:GitHubFile
您可以按照以下流程:
- 找出页面中所有的
input
标签元素,存入列表 - 遍历列表,使用
getAttribute()
方法获取属性id
并将其存储在另一个 Arraylist 中,比如你提到的AllFormInputIDs
。 - 现在您可以遍历 "AllFormInputIDs" 的列表来执行您想要的任何操作。
下面是上述过程的Java代码片段:
//Opening firefox driver instance, maximizing and assigining an implicit timeout of 20 seconds.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Navigating to the site
driver.get("");
//Fetching all the elements with tag 'input' and putting in a List
List<WebElement> List_Inputs = driver.findElements(By.tagName("input"));
//Creating an ArrayList object
ArrayList<String> AllFormInputIDs = new ArrayList<String>() ;
//Iterating throught the input ids in List_Inputs and fetching the 'id' attribute
//where 'id' value is not an empty string
for(WebElement inputID: List_Inputs){
if(!inputID.getAttribute("id").equals(""))
AllFormInputIDs.add(inputID.getAttribute("id"));
}
//Iterating over AllFormInputIDs where the fetched id's of all Input tags are present
//and printing them
int i=1;
for(String id: AllFormInputIDs){
System.out.println("ID "+(i++)+": "+id);
}