我有一个奇怪的案例,其中selenium chrome驱动程序getText()方法(java)为某些元素返回一个空字符串,即使它为其他元素返回一个非空字符串xpath.这是一个页面.
<div __gwt_cell="cell-gwt-uid-223" style="outline-style:none;">
<div>Text_1</div>
<div>Text_2</div>
<div>Text_3</div>
<div>Text_4</div>
<div>Text_5</div>
<div>Text_6</div>
</div>
Run Code Online (Sandbox Code Playgroud)
每个内标签的,我可以得到有效的返回值getTagName(),getLocation(),isEnabled(),和isDisplayed().但是,getText()为某些div返回一个空字符串.
此外,我注意到如果我使用mac chrome驱动程序,它始终是'Text_5',getText()返回一个空字符串.如果我使用Windows Chrome驱动程序,它始终是'Text_2',getText()返回一个空字符串.如果我使用firefox驱动程序,则getText()返回所有div的预期文本.
有没有其他人有这个困难?
在我的代码中,我使用这样的东西......
ArrayList<WebElement> list = (ArrayList<WebElement>) driver.findElements(By.xpath(“my xPath here”));
for (WebElement e: list) System.out.println(e.getText());
Run Code Online (Sandbox Code Playgroud)
如下所示,这是xPath我正在使用的实际情况.上面的页面代码段处理最后两个div.
//*[@class='gwt-DialogBox']//tr[contains(@class,'data-grid-table-row')]//td[contains(@class,'lms-assignment-selection-wizard-cell')]/div/div
Run Code Online (Sandbox Code Playgroud) 我对一个公共网站进行了一些测试,看看我是否能找到一些不同的Selenium CSS选择器的性能差异.我跑了一个有五个节点的集线器; mac/chrome/local,mac/safari/local,mac/ff/local,win7/ie9/localVM和win8/ie10,localVM.测试全部并行运行,试图模拟我通常如何运行它们.我惊讶地发现xPath选择器并没有成为我所期待的魔鬼.也许我的测试有点时髦吗?有人有任何见解吗?
这是测试代码......
int cycles = 500;
int yVal = 0;
getPage(“http://www.princeton.edu");
/* try an element that does not have an id*/
startStopwatch();
for (int i = 0; i < cycles; i++)
yVal = driver.findElementByCssSelector("a[href='/main/news/events/']").getLocation().y;
print("By CSS: " + elapsedSeconds());
startStopwatch();
for (int i = 0; i < cycles; i++)
yVal = driver.findElementByCssSelector("div[id='events'] a[href='/main/news/events/']").getLocation().y;
print("By CSS using id: " + elapsedSeconds());
startStopwatch();
for (int i = 0; i < cycles; i++)
yVal = driver.findElementByXPath("//a[@href=\'/main/news/events/']").getLocation().y;
print("By xPath: " + elapsedSeconds()); …Run Code Online (Sandbox Code Playgroud)