无法使用我知道HTML使用CSS选择器构建的所有元素定位器来定位元素

时间:2018-06-26 10:13:32

标签: java css selenium

这是我要查找财务报告的HTML页面。我尝试使用XPath,CSS选择器,但没有运气:

<div id='cssmenu'>
    <ul>                        
        <!-- ANALYSIS -->
        <div style="margin:10px">ANALYSIS</div>

        <li class='has-sub'><a href='javascript:void(0)'><div>Accruals</div></a>
            <ul>
                <li><a href='/financial/dailyAccrualMaintain.do?action=DAILY_MAINTAIN_OPEN&clear=true'><div>Daily</div></a></li>
                <li class='last'><a href='/financial/accrual.do?action=list1&clear=true'><div>Monthly</div></a></li>
            </ul>
        </li>
        <li><a href='/financial/financialReports.do?action=LIST_REPORTS&clear=true'><div>Financial Reports</div></a></li>
        <li><a href='/financial/validationReports.do?action=LIST_REPORTS&clear=true'><div>Validation Reports</div></a></li>
        <li><a href='/financial/financialCheck.do?action=OPEN&clear=true'><div>Financial Check</div></a></li>

    </ul>
</div>

我使用CSS选择器定位该元素的Java代码,还使用了waitl直到可见的元素:

private final String finacialReportpath = "div#cssmenu li:nth-child(3)"; 
    @FindBy(css = finacialReportpath)
    private WebElement finacialReport;

public void clickfinacialReport() {

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(finacialReportpath)));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(finacialReportpath)));

        this.finacialReport.click();

}

2 个答案:

答案 0 :(得分:1)

您可以使用此XPath:

//a[contains(.,'Financial Reports')]

如果仍然无法找到它,则要确保是否在任何帧中。
如果框架中存在,请切换到该框架,然后尝试查找该元素。

答案 1 :(得分:0)

您似乎想单击链接,但您选择的是@objc func draggedView1(_ sender:UIPanGestureRecognizer){ guard let object = sender.view else { return } let panelRadius = self.backPanel.frame.size.width/2 self.view.bringSubview(toFront: object1) let translation = sender.translation(in: self.view) var newX = object.center.x + translation.x var newY = object.center.y + translation.y let r = sqrt((newX - backPanel.center.x) * (newX - backPanel.center.x) + (newY - backPanel.center.y) * (newY - backPanel.center.y)) if (r > panelRadius ){ newX = object.center.x newY = object.center.y } object.center = CGPoint(x: newX, y: newY) sender.setTranslation(CGPoint.zero, in: self.view) } 。也许选择链接(它也具有一组相当独特的属性的优点):

li

private final String finacialReportpath = "a[href^='/financial/financialReports.do']"; // Changed -------------------------------^ @FindBy(css = finacialReportpath) private WebElement finacialReport; public void clickfinacialReport(){ wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(finacialReportpath))); wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(finacialReportpath))); this.finacialReport.click(); } 的意思是“ starts with”。