【问题标题】:Not able to click on element of table in selenium无法单击硒中的表格元素
【发布时间】:2016-08-23 10:47:02
【问题描述】:

我想点击表格中的一个元素,该元素的html代码如下

<A onclick="return A_MENUS[0].onclick(4);" onmouseover=A_MENUS[0].onmouseover(4); onmousedown=A_MENUS[0].onmousedown(4); onmouseout=A_MENUS[0].onmouseout(4); id=e0_4o class=m0l1oout style="HEIGHT: 20px; WIDTH: 250px; POSITION: absolute; LEFT: 600px; Z-INDEX: 4; TOP: 98px; VISIBILITY: hidden" href="WebMenu.aspx"><DIV id=e0_4i class=m0l1iout>Start Proposal</DIV></A>  

以下是屏幕短,点击承保后我无法点击电机。该代码在一台电脑上工作,但在另一台电脑上不工作。请帮帮我。
代码如下

wait = new WebDriverWait(wDriver, 150);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("home")));
wDriver.findElement(By.id("home")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("e0_5991o")));
wDriver.findElement(By.id("e0_5991o")).click();
customSleep(1000);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("e0_6101o")));
wDriver.findElement(By.id("e0_6101o")).click();
customSleep(1000);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("e0_6128o")));
wDriver.findElement(By.id("e0_6128o")).click(); 

【问题讨论】:

  • 你试过最大化浏览器窗口吗?

标签: java selenium selenium-webdriver selenium-grid


【解决方案1】:

先试试这个:

 (JavascriptExecutor(webdriver)).executeScript("document.getElementById('ur id').click();");

如果这不起作用,试试这个:

WebElement elem = driver.findElement(By.xpath("ur xpath"));//u may use by id or by class as ur wish
String makeVisible = "arguments[0].style.visibility='visible';";
((JavascriptExecutor) driver).executeScript(makeVisible, elem);

然后像这样点击你的元素

elem.click();

如果该元素在屏幕上不可见,则首先滚动到该元素,如下所示:

JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement elem = driver.findElement(By.xpath("ur xpath"));

 //this line will scroll down to make element visible
js.executeScript("window.scrollTo(" + elem.getLocation().x + "," +(elem.getLocation().y- 100) + ");");

在你的屏幕中可见的元素之后,而不是通过普通的驱动程序点击或 javascript 点击来点击。

【讨论】:

  • 谢谢,但它没有工作给出以下错误 org.openqa.selenium.WebDriverException: JavaScript error Code is JavascriptExecutor jsExec = (JavascriptExecutor)wDriver; jsExec.executeScript("document.getElementById('e0_5991o')[0].click();");
  • 对不起,我输入错误了……我更新了代码
  • 您也可以尝试其他替代解决方案。
  • 对不起,但不工作错误消失了,但 'jsExec.executeScript("document.getElementById('e0_5991o').click();");'没有点击元素。请帮忙
猜你喜欢
  • 2018-11-21
  • 2020-05-20
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
相关资源
最近更新 更多