【问题标题】:Element not clickable using Selenium webdriver with Chrome (same code:works fine using firefox)使用带有 Chrome 的 Selenium webdriver 无法点击元素(相同的代码:使用 firefox 可以正常工作)
【发布时间】:2015-07-14 00:37:54
【问题描述】:

使用 selenium-2.44.tar.gz 和 chrome 自动化测试用例:很奇怪,我的代码使用 Firefox 33 可以正常工作,但使用 Google Chrome 失败:

"WebDriverException: 消息: u'unknown error: Element is not 在点 (57, 161) 处可点击。其他元素会收到点击:...\n (会话信息:chrome=42.0.2311.135)\n(驱动程序信息: chromedriver=2.9.248315,平台=Windows NT 6.1 SP1 x86_64)'" self.driver.find_element_by_xpath(".//[@id='searchMessagstoryBtn']").click()

有什么想法吗?它不应该更好地与 chrome one 一起使用,因为 webdriver 现在是谷歌代码还是我错了!!!

【问题讨论】:

标签: google-chrome selenium-webdriver


【解决方案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()

【讨论】:

    【解决方案2】:

    这似乎是 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();

    希望这对遇到此问题的其他人有所帮助

    【讨论】:

    • 您好,谢谢,您有没有机会给我python中的代码和任何需要的导入谢谢。
    【解决方案3】:
    @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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 2016-05-23
      • 2014-09-09
      • 2020-11-10
      • 2022-12-09
      相关资源
      最近更新 更多