2017-06-22 199 views
2

我已經讀過關於這個主題的所有其他問題,這裏有很多。我嘗試了一些,但是在代碼中找不到錯誤。Selenium找不到id和xpath的元素

我也嘗試添加一個計時器來等待頁面加載。

下面的HTML代碼和Java:

HTML:

<form id="myform" method="get" action=""> 
 
    <input type="hidden" name="something1" id="something1.1" /> 
 
    <input type="hidden" name="something2" value="" /> 
 
\t <table> 
 
     <tr> 
 
      <td><label>Name: </label></td> 
 
      <td><select name="name"> 
 
       <option selected="selected" value="1000">FirstNameOnly</option> 
 
      </select></td> 
 
     </tr> 
 
     <tr> 
 
      <td><label>Direction: </label></td> 
 
      <td><select name="Direction"> 
 
       <option selected="selected" value="">Choose One</option> 
 
       <option value="UP">UP</option> 
 
      </select></td> 
 
     </tr> 
 
     <tr> 
 
      <td colspan="2"><label>Time: </label></td> 
 
     </tr> 
 
     <tr> 
 
      <td><label>From: </label></td> 
 
      <td><input type="text" value="" name="from" id="id6"/> 
 
     </tr> 
 
     <tr> 
 
      <td><label>To: </label></td> 
 
      <td><input type="text" value="" name="to" id="id7"/> 
 
     </tr> 
 
     <tr> 
 
      <td><label>File type: </label></td> 
 
      <td><span id="id8"> 
 
      <input name="fileType" type="radio" checked="checked" value="0" id="id8-0"/><label for="id8-0">Excel</label> 
 
      <input name="fileType" type="radio" value="1" id="id8-1"/><label for="id8-1">CSV</label> 
 
      </span></td> 
 
     </tr> 
 
     <tr> 
 
     <td></td> 
 
     <td> 
 
      <input type="submit" name="p::submit" id="id9" value="Preview"> 
 
      <input type="submit" name="download" id="ida" value="Download"> 
 
     </td> 
 
     </tr> 
 
    </table> 
 
</form>

JAVA:

public void HeadlessChromeStartDownload(){ 

    System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe"); 

    ChromeOptions options = new ChromeOptions(); 
    if (ValidateOS.isWindows()){ 
     options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe"); 
     System.out.println("Windows system"); 
    } else if (ValidateOS.isUnix()){ 
     options.setBinary("/path/to/chrome/not/yet/added"); 
    } 

    options.addArguments("--headless --disable-gpu"); 
    ChromeDriver driver = new ChromeDriver(options); 

    driver.get("http://localhost/that-test-page.html"); 
    //WebElement timer = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("id8-1"))); 

    WebElement select1 = driver.findElementByName("FirstNameOnly"); 
    Select field1 = new Select(select1); 
    field1.selectByIndex(1); 

    WebElement select2 = driver.findElementByName("Direction"); 
    Select field2 = new Select(select2); 
    field2.selectByIndex(1); 

    driver.findElementByName("from").sendKeys("21/06/2017"); 

    driver.findElementByName("to").sendKeys("22/06/2017"); 

    /*File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
    try { 
     FileUtils.copyFile(scrFile, new File("C:\\screen1.jpg")); 
     System.out.println("Screen saved"); 
    } catch (IOException e) { System.out.println("Screen NOT saved"); } 
    */ 

    //driver.findElement(By.id("id8-1")).click(); 
    driver.findElement(By.xpath("//form[1]/input[6]")).click(); 

    //driver.findElementById("ida").click(); 

    driver.quit(); 
} 

這真的,如果我用也無所謂:

driver.findElement(By.id("id8-1")).click(); 

driver.findElementById("id8-1").click(); 

driver.findElement(By.xpath("//form[1]/input[6]")).click(); 

我不能讓硒點擊該單選按鈕。 其餘的代碼也是如此,實際上我用的是findElementByName這顯然不是最好的選擇。

感謝誰知道這個代碼有什麼問題! (:(:

UPDATE1:

所以,我無法解釋昨天發生了什麼網站我想試驗使用id8-1爲單選按鈕今天它是id3-1。,並且兩個溶液:driver.findElement(By.cssSelector("input[value='1']")).click();或地雷:driver.findElement(By.id("id3-1")).click();工作

我驚訝這顯然是昨日8 不過,我不知道,如果使用cssSelector解決方案是最好的,因爲我想。使用ID。 我upvoted所有的答案,因爲所有是你但我希望使用身份證,所以我使用我的代碼..在你身邊的更新情況下,我會選擇它(:(

感謝所有!

回答

2

嘗試以下操作:

driver.findElement(By.xpath("//input[@name='fileType' AND @id='id8-1']")).click(); 
+0

謝謝!我會明天嘗試它,因爲我已經離開辦公室了(: – Nihvel

+0

'異常在線程中「main」org.openqa.selenium.InvalidSelectorException:無效的選擇器:無法找到xpath表達式的元素// input [@ name ='fileType 'AND'id ='id8-1'],因爲有以下錯誤: SyntaxError:未能在'Document'上執行'evaluate':字符串'//輸入[@ name ='fileType'AND @ id ='id8 -1']'不是有效的XPath表達式.'): – Nihvel

+0

正在更新主要問題.. – Nihvel

1

你可以做到這一點,而不使用XPath也

driver.findElement(By.cssSelector("input[value='1 0r 0']")).click(); 

您可以選擇您要選擇

+0

謝謝!我現在無法嘗試,我離開了辦公室。我明天會繼續! – Nihvel

+0

更新主要問題! – Nihvel

2

0或1是否硒拋出一個錯誤?當嘗試點擊DOM中不存在的元素時,您會看到類似'元素不再附加到DOM的 '的錯誤。

比使用wait更好,是編寫一個方法來驗證元素是否可用。請嘗試以下之一:

private void waitUntilElementExistsAndIsVisible(final By by) { 
    new FluentWait<WebDriver>(driver).withTimeout(DEFAULT_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS) 
     .pollingEvery(DEFAULT_SLEEP_TIME_IN_SECONDS, TimeUnit.SECONDS).ignoring(NoSuchElementException.class) 
     .until(new ExpectedCondition<Boolean>() { 
     public Boolean apply(WebDriver wd) { 
      return wd.findElement(by).isDisplayed(); 
     } 
     }); 
} 

與元素執行一些動作之前使用它:

waitUntilElementExistsAndIsVisible(By.id("id8-1")); 
driver.findElement(By.id("id8-1")).click(); 

另一個解決方法是使用Action方法。嘗試將鼠標移動到封閉標記(<span id="id8">),然後單擊所需的元素:

Actions builder = new Actions(driver); 
builder.moveToElement(driver.findElement(By.id("id8"))).perform(); 
waitUntilElementExistsAndIsVisible(By.id("id8-1")); 
driver.findElement(By.id("id8-1")).click(); 
+0

錯誤是:線程中的異常「main」org.openqa.selenium.NoSuchElementException:no such element:Unable to locate element:{「method」:「id」,「selector」:「id8-1」}'雖然我知道有,因爲我從頁面源中看到它。但我喜歡你給我的這種方法,所以我將在代碼中實現!謝謝! – Nihvel

0

當硒駕駛IE,我遇到了一個問題,即driver.find_element_by_id("elem_id")無法找到它,但在JavaScript運行document.getElementById("elem_id") IE的開發者控制檯可以。就我而言,我正在提取一個值。所以我就這樣做了driver.execute_script('return document.getElementById("elem_id").outerText')