Selenium 2 /WebDriver 迁移问题 WebDriver 未选择正确的元素

发布于 2024-12-01 11:39:22 字数 789 浏览 0 评论 0原文

我正在将我的测试套件从 Selenium 1 迁移到 WebDriver。我在单击包含美国可点击州地图的页面上的元素时遇到了问题。

使用 Selenium 1,我将执行以下操作以从地图中选择特定状态。

selenium.click("css=area[alt=North Carolina]");

在 Selenium 2 中,我将其转换为

driver.findElement(By.cssSelector("area[alt=North Carolina]")).click();

WebDriver 实际上选择了不同的状态。这不是时间问题,选择了一个状态,这只是不正确的状态。我尝试过将 cssselector 切换为 xpath ,结果相同。

不确定这是否与此问题相关

操作系统:Win XP

浏览器:在 IE 8 和 FF 5 上测试6

Selenium:2.5.0

页面代码示例

<area alt="California" shape="POLY" coords="10,60,29,68,25,98,56,146,51,167,17,138,3,70"   href="javascript:LoadCategory('CA');">

I am in the process of migrating my test suite from the Selenium 1 to WebDriver. I've come across a problem clicking on elements on a page which contains a map of the USA clickable states.

Using Selenium 1 I would do the following to select a particular state from the map.

selenium.click("css=area[alt=North Carolina]");

In Selenium 2 I'm converting this to

driver.findElement(By.cssSelector("area[alt=North Carolina]")).click();

WebDriver actually selects a different State. This is not a timing issue, a state is selected it is just the incorrect state. I've tried switching out cssselector for xpath with the same result.

Not sure if this is related to this issue

OS : Win XP

Browsers: Tested on IE 8 and FF 5 & 6

Selenium : 2.5.0

Example of page code

<area alt="California" shape="POLY" coords="10,60,29,68,25,98,56,146,51,167,17,138,3,70"   href="javascript:LoadCategory('CA');">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2024-12-08 11:39:22

查看您提供的页面的 HTML,似乎有针对 CA、AZ、OH 等州的单独页面。还有一些其他州(其中大多数)如 NM、MT 等都有公共页面。就 webdriver 而言,我不确定为什么它没有点击正确的元素。我写了类似下面的内容,对我有用。您必须将这个逻辑扩展到其他状态。

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
driver.get("http://www.servsafe.com/catalog/starterscategories.aspx");
Selenium selenium = new WebDriverBackedSelenium(driver, driver.getCurrentUrl());
selenium.windowMaximize();
((JavascriptExecutor)driver).executeScript("javascript:LoadCategory('CA');");

同样,您可以对具有单独页面的其他州执行此操作,例如

((JavascriptExecutor)driver).executeScript("javascript:LoadCategory('AZ');");
((JavascriptExecutor)driver).executeScript("javascript:LoadCategory('FL');");

对于具有公共页面的州,

((JavascriptExecutor)driver).executeScript("javascript:LoadCategory();");

Looking at the HTML for the page you provided, it seems that there are individual pages for states like CA,AZ,OH etc. There are some other states (majority of them) like NM, MT etc all have common pages. As far as webdriver is concerned, I am not sure why it's not clicking on the right element. I wrote something like below that works for me. You will have to extend this logic for other states.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
driver.get("http://www.servsafe.com/catalog/starterscategories.aspx");
Selenium selenium = new WebDriverBackedSelenium(driver, driver.getCurrentUrl());
selenium.windowMaximize();
((JavascriptExecutor)driver).executeScript("javascript:LoadCategory('CA');");

Similarly, you can do it for other states that have individual pages, like

((JavascriptExecutor)driver).executeScript("javascript:LoadCategory('AZ');");
((JavascriptExecutor)driver).executeScript("javascript:LoadCategory('FL');");

For states that have common pages,

((JavascriptExecutor)driver).executeScript("javascript:LoadCategory();");
伴随着你 2024-12-08 11:39:22

我建议使用 Javascript click() 方法。

例子,

new WebDriverBackedSelenium(driver,"").assignId("css=area[alt='California']", "California");
((JavascriptExecutor) driver).executeScript("document.getElementById('California').click()");

I make a suggestion to use Javascript click() method.

example,

new WebDriverBackedSelenium(driver,"").assignId("css=area[alt='California']", "California");
((JavascriptExecutor) driver).executeScript("document.getElementById('California').click()");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文