I read already all the other questions about this topic, there are a lot. I tried some but I do not find error in the code.
I tried adding a timer too to wait for the page to load.
Below the html code and the java:
HTML:
<form id="myform" method="get" action="">
<input type="hidden" name="something1" id="something1.1" />
<input type="hidden" name="something2" value="" />
<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();
}
It really does not matter if I use:
driver.findElement(By.id("id8-1")).click();
or
driver.findElementById("id8-1").click();
or
driver.findElement(By.xpath("//form[1]/input[6]")).click();
I can't get Selenium to click on that radio button.
And the same goes for the rest of the code, in fact I used findElementByName which obviously is not the best choice.
Thanks to anyone who knows what is wrong with this code!! (: (:
So, I can not explain what happened yesterday. The website I am trying to test was using id8-1 for that radio button. Today it is id3-1, and both the solution with: driver.findElement(By.cssSelector("input[value='1']")).click(); or mine: driver.findElement(By.id("id3-1")).click(); worked.
I'm astonished. It was clearly 8 yesterday. Still, I do not know if using the cssSelector solution is the best, because I want to work with the IDs. I upvoted all the answers because all are useful but I wish to use IDs so I'm using my code.. In case of updates by your side I will choose it the one (: (:
Thanks to all!!!
When driving IE with Selenium, I encountered a problem where driver.find_element_by_id("elem_id") can't find it but running javascript document.getElementById("elem_id") in IE's developer console can. In my case, I was extracting a value. So I worked it around by doing driver.execute_script('return document.getElementById("elem_id").outerText')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With