Java Seleniun Hashmap(字符串,WebElement)在传递字符串时不返回WebElement
我编写了一个java类(CustomerHistoryMapping),它包含一个HashMap(String,WebElement),如下所示(代码的第一部分) 我的总体目标是能够使用给定的WebElement,具体取决于传递给HashMap类的字符串Java Seleniun Hashmap(字符串,WebElement)在传递字符串时不返回WebElement,java,selenium,hashmap,Java,Selenium,Hashmap,我编写了一个java类(CustomerHistoryMapping),它包含一个HashMap(String,WebElement),如下所示(代码的第一部分) 我的总体目标是能够使用给定的WebElement,具体取决于传递给HashMap类的字符串 package com.xxxxx.data; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import java.uti
package com.xxxxx.data;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import java.util.HashMap;
import java.util.Map;
public class CustomerHistoryMapping {
@FindBy(xpath = "(//td/span[text()='Preferred Name']/../following-sibling::td[3]/span/span)[1]")
public WebElement txaNewPreferredNameValue;
@FindBy(xpath = "(//td/span[text()='Mobile']/../following-sibling::td[3]/span/span)[1]")
public WebElement txaNewMobileValue;
@FindBy(xpath = "(//td/span[text()='Email']/../following-sibling::td[3]/span/span)[1]")
public WebElement txaNewEmailValue;
@FindBy(xpath = "(//td/span[text()='Mailing Street']/../following-sibling::td[3]/span/span)[1]")
public WebElement txaNewMailingStreetValue;
@FindBy(xpath = "(//td/span[text()='Mailing City']/../following-sibling::td[3]/span/span)[1]")
public WebElement txaNewMailingCityValue;
@FindBy(xpath = "(//td/span[text()='Mailing Zip/Postal Code']/../following-sibling::td[3]/span/span)[1]")
public WebElement txaNewMailingPostCodeValue;
@FindBy(xpath = "(//td/span[text()='Mailing State/Province']/../following-sibling::td[3]/span/span)[1]")
public WebElement txaNewMailingStateValue;
Map<String, WebElement> mapping = new HashMap<String, WebElement>();
public CustomerHistoryMapping() {
mapping.put("preferredName", txaNewPreferredNameValue);
mapping.put("mobile", txaNewMobileValue);
mapping.put("email", txaNewEmailValue);
mapping.put("address", txaNewMailingStreetValue);
mapping.put("city", txaNewMailingCityValue);
mapping.put("postcode", txaNewMailingPostCodeValue);
mapping.put("state",txaNewMailingStateValue);
}
public WebElement getValue(String mapKey) {
return mapping.get(mapKey);
}
}
所以HashMap中的WebElement并没有被撤回——知道为什么吗 您应该添加
PageFactory.initElements(…)
,以便使用页面上的元素初始化字段。可以将其添加为构造函数的最后一行
除非您使用了initelements
,否则所有字段都将引用null
objCustomerHistoryMapping = new CustomerHistoryMapping();
WebElement ele = objCustomerHistoryMapping.getValue("preferredName");
logger.info("Str is: preferredName, And ele is: " + ele);