2017-08-10 333 views
1

我編寫了以下腳本來練習Selenium瀏覽器自動化。它訪問Steam的網站並更改您的Steam用戶名。如果在沒有Chrome的無頭模式下運行,它會很好地工作,但如果以options.addArguments("headless")開頭,則無法找到第一個元素。代碼:Selenium webdriver無法定位Chrome無頭模式中的元素

@Test 
public void steamPowered() throws IOException { 

    System.setProperty("webdriver.chrome.driver", "C:\\Driver\\chromedriver.exe"); 

    ChromeOptions options = new ChromeOptions(); 
    options.addArguments("--user-data-dir=C://Users/Evan/Downloads/Profile8Aug17"); 
    options.addArguments("headless"); 
    options.addArguments("window-size=1200x600"); 

    ChromeDriver driver = new ChromeDriver(options); 

    driver.navigate().to("https://store.steampowered.com/"); 

    WebElement element = (new WebDriverWait(driver, 10)) 
      .until(ExpectedConditions.presenceOfElementLocated(By.id("account_pulldown"))); 
    element.click(); 

    element = (new WebDriverWait(driver, 10)) 
      .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"account_dropdown\"]/div/a[4]"))); 
    element.click(); 

    element = (new WebDriverWait(driver, 10)) 
      .until(ExpectedConditions.presenceOfElementLocated(By.xpath 
      ("/html/body/div[1]/div[7]/div[2]/div[1]/div[1]/div/div/div/div[3]/div[2]/a/span"))); 
    element.click(); 

    element = (new WebDriverWait(driver, 10)) 
      .until(ExpectedConditions.presenceOfElementLocated(By.id("personaName"))); 
    element.clear(); 
    element.sendKeys(scramble(USERNAME)); 

    driver.findElement(By.xpath("//span[text()='Save Changes']")).click(); 

    driver.quit(); 
} 

}

在的IntelliJ的打印輸出時崩潰:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate 
element: {"method":"id","selector":"account_pulldown"} 
    (Session info: headless chrome=60.0.3112.90) 
    (Driver info: chromedriver=2.31.488763 
(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 
x86_64) (WARNING: The server did not provide any stacktrace information) 

這是令人困惑,因爲下面的代碼工作完美(複製自https://medium.com/@eliasnogueira/running-selenium-tests-with-chrome-headless-5edd624efb92粘貼)

@Test 
public void testExecution() throws IOException { 
    System.setProperty("webdriver.chrome.driver", "C:\\Driver\\chromedriver.exe"); 

    // Add options to Google Chrome. The window-size is important for responsive sites 
    ChromeOptions options = new ChromeOptions(); 
    options.addArguments("--user-data-dir=C://Users/Evan/Downloads/Profile8Aug17"); 
    options.addArguments("headless"); 
    options.addArguments("window-size=1200x600"); 

    WebDriver driver = new ChromeDriver(options); 
    driver.get("http://seleniumhq.org"); 

    // a guarantee that the test was really executed 
    assertTrue(driver.findElement(By.id("q")).isDisplayed()); 

    driver.quit(); 
} 

那麼我在這裏錯過了什麼?我沒有看到兩者之間有什麼嚴重的區別。什麼阻止我的腳本在無頭模式下查找頁面元素?

+0

獲取無頭模式PageSource,看看有什麼不對嗎 –

+0

什麼是'「--user-data-dir = C:// Users/Evan/Downloads/Profile8Aug17」'關於什麼?您是否嘗試加載Chrome個人資料? – DebanjanB

+0

@DebanjanB沒錯。這是我遇到的問題(可能是天真的)解決方案。使用我的默認配置文件時,chrome實例無法導航。鉻窗口將打開,但然後凍結並關閉一分鐘左右後,intellij引發錯誤'org.openqa.selenium.WebDriverException:未知錯誤:Chrome無法啓動:崩潰'。所以我將驅動程序指向了像/ Downloads /這樣的任意位置,直到我嘗試了無頭模式之前,它創建了一個我一直使用的新配置文件。 – evanyoho

回答

0

爲了使用在Java中使用ChromeOptions你應該使用--headless無頭鍍鉻,你可能只是更換:

options.addArguments("headless"); 

有:

options.addArguments("--headless"); 
+0

這沒有幫助,當鉻窗口打開時,一切正常,當我添加.addArguments(「 - 無頭」)或.setHeadless(真)我得到: 「org.openqa.selenium .NoSuchElementException:沒有這樣的元素「 – hipokito

相關問題