简体   繁体   English

Java,用于查找Web元素的相对xpath

[英]Java, relative xpath to find web elements

I am trying to go through a table (id="campaignAllAvailableList") and iterate through a list of titles: ( xpath = .//*[@id='campaignAllAvailableList']/li/div/div/h3 ) to see if it matches with the one I want. 我试图通过一个表(id =“ campaignAllAvailableList”)并遍历标题列表:( xpath = .//*[@id='campaignAllAvailableList']/li/div/div/h3 )以查看是否它与我想要的一个相匹配。

If it does, then I click onto the corresponding button: 如果是这样,那么我单击相应的按钮:

(xpath = .//*[@id='campaignAllAvailableList']/li/div/div[3]/a ) (xpath = .//*[@id='campaignAllAvailableList']/li/div/div[3]/a

 <div id="campaignListWrapper" class="campaignOverviewSectionBody campaignListing" style=""> <ul id="campaignAllAvailableList" class="campaignList" style="display: block;"> <li> <div class="timetableCard clearedGroup"> <div class="timetableCardHeader helpAnchor"> <h3 class="timetableCardTitle">November Campaign metaTitle</h3> <img class="helpIcon" src="/images/icons/info-icon.png"/> <div class="popUp helpPopUp"> </div> <div class="timetableCardBody"> <div class="timetableCardActionBar clearedGroup"> </div> </li> <li> <div class="timetableCard clearedGroup"> <div class="timetableCardHeader helpAnchor"> <h3 class="timetableCardTitle">qwe</h3> <img class="helpIcon" src="/images/icons/info-icon.png"/> <div class="popUp helpPopUp"> </div> <div class="timetableCardBody"> <div class="timetableCardActionBar clearedGroup"> </div> </li> 

This is the java code I have right now: 这是我现在拥有的Java代码:

public class PromotionsPage extends MainPageTemplate
{
    @FindBy(xpath=".//*[@id='campaignAllAvailableList']/li/div")
    List<WebElement> campaignTable;

public PromotionsPage(WebDriver d) throws PageValidationException
{
    super(d);
}

public void clickSpecificGetStartedButton(String metadataTitle)
{

    for (WebElement el : campaignTable)
    {
        WebElement metadataTitleTextElement = el.findElement(By.xpath(".//div/h3"));
        if (metadataTitle == metadataTitleTextElement.getText())
        {
            WebElement getStartedButton = el.findElement(By.xpath(".//div[3]/a"));
                getStartedButton.click();
            }
        }
    }
}

It is failing at "for (WebElement el : campaignTable)", I think it is not reading campaignTable as a List properly... 它在“ for(WebElement el:campaignTable)”处失败,我认为它没有正确地将CampaignTable作为List读取...

I tried finding the list of title directly 我尝试直接找到标题列表

@FindBy(xpath=".//*[@id='campaignAllAvailableList']/li/div/div/h3")
List<WebElement> campaignTable;

and do 并做

for (WebElement el : campaignTable)
        {
            System.out.println(el.getText());
}

but this is giving an error too, java.lang.IllegalArgumentException: object is not an instance of declaring class 但这也给一个错误,java.lang.IllegalArgumentException:对象不是声明类的实例

insert a break; break; after clicking the element to escape from the loop. 单击该元素以退出循环后。 See below: 见下文:

public void clickSpecificGetStartedButton(String metadataTitle)
{
for (WebElement el : campaignTable)
{
    WebElement metadataTitleTextElement = el.findElement(By.xpath(".//div/h3"));
    if (metadataTitle == metadataTitleTextElement.getText())
    {
        WebElement getStartedButton = el.findElement(By.xpath(".//div[3]/a"));
            getStartedButton.click();
            break;
        }
    }
}
}

I fixed it, but I am still not sure why the original code doesnt work 我修复了它,但是我仍然不确定为什么原始代码不起作用

List<WebElement> campaignTable = driver.findElements(By.xpath(".//*[@id='campaignAllAvailableList']/li/div"));  
for (WebElement el : campaignTable)
{
    String metadataTitle = el.findElement(By.xpath(".//div/h3")).getText();

    if (target.compareTo(metadataTitle)==0)
    {
        WebElement getStartedButton = el.findElement(By.xpath(".//div[3]/a"));
        getStartedButton.click();
        break;
    }
}``

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM