使用Selenium webdriver与Chrome无法点击的元素(相同代码:使用firefox工作正常)

时间:2015-05-04 14:56:40

标签: google-chrome selenium-webdriver

使用selenium-2.44.tar.gz和chrome自动化测试用例:非常奇怪我的代码使用Firefox 33工作正常但无法使用谷歌浏览器:

  

“WebDriverException:消息:u'unknown错误:元素不是   可点击(57,161)。其他元素将收到点击:... \ n   (会话信息:chrome = 42.0.2311.135)\ n(驱动程序信息:   chromedriver = 2.9.248315,platform = Windows NT 6.1 SP1 x86_64)'“   self.driver.find_element_by_xpath( “.//[@ ID = 'searchMessagstoryBtn']”)。单击()

任何想法?由于webdriver现在是Google代码,或者我错了!!!

3 个答案:

答案 0 :(得分:1)

与firefox不同,要使用chromedriver,您必须从http://www.seleniumhq.org/download/下载chromedriver,并且必须在代码中提供路径。
您可以使用以下代码,在java或python中使用chrome驱动程序
 爪哇:

public void testGoogleSearch() {
  // Optional, if not specified, WebDriver will search your path for chromedriver.
  System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

  WebDriver driver = new ChromeDriver();
  driver.get("http://www.google.com/xhtml");
  Thread.sleep(5000);  // Let the user actually see something!
  WebElement searchBox = driver.findElement(By.name("q"));
  searchBox.sendKeys("ChromeDriver");
  searchBox.submit();
  Thread.sleep(5000);  // Let the user actually see something!
  driver.quit();
}

的Python:

import time
from selenium import webdriver

driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

答案 1 :(得分:0)

这似乎是ChromeDriver中的一个错误。 http://code.google.com/p/selenium/issues/detail?id=2766

尝试使用表单#27中的此变通方法解决方案。我希望它会对你有所帮助:

  

我也遇到了同样的问题Chrome和点击该元素   在Firefox中工作正常,但在Chrome中没有...修复非常简单   但是,您所要做的就是将元素滚动到视图之前   点击它,你就不会在Chrome中遇到这个问题。这是   我使用的代码:

     

IWebElement elementToClick =;

     

//将浏览器滚动到元素的Y位置(驱动程序为   IJavaScriptExecutor).ExecuteScript(的String.Format(“window.scrollTo(0,   {0});“,elementToClick.Location.Y));

     

//单击元素elementToClick.Click();

     

希望这有助于遇到此问题的其他任何人

答案 2 :(得分:0)

@FindBy(xpath = "//[@id='searchMessagstoryBtn']")
private WebElement Search_btn;

public WebElement getSearchBtnClick() {
    return Search_btn;
}

public void Click_Search_Btn() {
    //click the search button
    TimeUnit.SECONDS.sleep(3);
    getSearchBtnClick().click();
}