gpt4 book ai didi

java - Selenium Hashmap(String, WebElement) 传递 String 时不返回 WebElement

转载 作者:行者123 更新时间:2023-12-01 18:01:28 26 4
gpt4 key购买 nike

我编写了一个java类(CustomerHistoryMapping),其中包含一个HashMap(String,WebElement),如下面的代码(代码的第一部分)我的总体目标是能够使用给定的 WebElement,具体取决于我传递给 HashMap 类的字符串。

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);
}

}

但是,当我调用 getValue("preferredName") - (参见第二个代码片段)并检查日志时,打印的内容如下:

Str 为:preferredName,ele 为:null

      objCustomerHistoryMapping = new CustomerHistoryMapping();
WebElement ele = objCustomerHistoryMapping.getValue("preferredName");
logger.info("Str is: preferredName, And ele is: " + ele);

所以 HashMap 中的 WebElement 没有被拉回 - 有什么想法吗?

最佳答案

您应该添加 PageFactory.initElements(...) 以便使用页面上的元素初始化字段。您可以将其添加为构造函数的最后一行。

除非您使用了 initelements,否则您的所有字段都将引用 null

关于java - Selenium Hashmap(String, WebElement) 传递 String 时不返回 WebElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60619896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com