我无法使用定位器找到chrome下载的文件名。我需要得到文件名,以进一步自动化,以验证内容。
元素HTML:
<a is="action-link" id="file-link" focus-row-control="" focus-type="fileLink" tabindex="0" role="link" href="https://energyfuture.cgi.com/pasdemo/operations/QueryDisplay!displayQuery.action?QUERY_TIMESTAMP=1614571607262" class="">Facility Balance_1614571607828.xlsx</a>在这里,文件名的数量是动态的,但前缀工具余额将是恒定的。
我尝试过的代码:
driver.navigate().to("chrome://downloads/");
String Text = driver.findElement(By.xpath("//*[@id='file-link']")).getText();或
String AttributeData = driver.findElement(By.xpath("//*[contains(text(),'Facility Balance')]")).getAttribute("linkText");发布于 2021-03-06 03:52:03
对于初学者来说
.getAttribute("linkText");将不起作用,因为目标节点不包含名为linkText的属性。
第二点,
By.xpath("//*[@id='file-link']")如果页面上有多个具有相同ID的元素(包括弹出窗口),则可能不起作用。考虑将您的xpath改进为更明确。例如,By.xpath("//a[@id='file-link' and ...]"),其中...是附加条件,它将使您的搜索细化为只找到一个条件。
出于好奇,您是否直接在浏览器开发人员的工具中尝试了Xpath?在尝试编写这样的代码之前,应该始终尝试计算XPath表达式,以确保1)它们是准确的,2)它们不会有歧义。不幸的是,考虑到张贴的信息量,这是我能给你的帮助的范围。
https://stackoverflow.com/questions/66498698
复制相似问题