Milky Milky - 4 months ago 34
Java Question

How to identify and click to the button in selenium webdriver when button does not have id and title, but has hover state?

Can anyone tell me how I can identify and click to the button which does not have title?

<div class="ui-fileupload-buttonbar ui-widget-header ui-corner-top">
<span class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left ui-fileupload-choose">
<span class="ui-button-icon-left ui-icon ui-c ui-icon-plusthick"></span>
<span class="ui-button-text ui-c">Browse</span>
<input id="j_idt126:attachments:j_idt128_input" name="j_idt126:attachments:j_idt128_input" type="file">


I have tried by below code but it is not working

By.xpath("//button[@type='button']/span[text()='Browse']"))).click();


How can I solve this problem in Selenium WebDriver with Java? Will be grateful for any help.

Answer

If you click the file upload input, you'll get a system file upload window that you cannot control.

Instead locate the element and sends the absolute path to the file you want to upload:

WebElement uploadInput = driver.findElement(By.xpath("//span[. = 'Browse']/following-sibling::input[@type='file']"));
uploadInput.sendKeys("/absolute/path/to/a/file");

Here we locating the input relying on the previous sibling's Browse text.

Comments