我已經讀過關於這個主題的所有其他問題,這裏有很多。我嘗試了一些,但是在代碼中找不到錯誤。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所有的答案,因爲所有是你但我希望使用身份證,所以我使用我的代碼..在你身邊的更新情況下,我會選擇它(:(
感謝所有!
謝謝!我會明天嘗試它,因爲我已經離開辦公室了(: – Nihvel
'異常在線程中「main」org.openqa.selenium.InvalidSelectorException:無效的選擇器:無法找到xpath表達式的元素// input [@ name ='fileType 'AND'id ='id8-1'],因爲有以下錯誤: SyntaxError:未能在'Document'上執行'evaluate':字符串'//輸入[@ name ='fileType'AND @ id ='id8 -1']'不是有效的XPath表達式.'): – Nihvel
正在更新主要問題.. – Nihvel