TrungNguyen
TrungNguyen

Reputation: 31

Selenium Webdriver - Unable to locate button with negative value of top

I am trying to open the popup dialog then enter file location to upload file but the below button is not visible when executing webdriver.

The top parameter of the button has negative value (-5000px) and when the mouse is moving, this value is changed accordingly.

Button:

<form><iframe onload="ajax.hideLoader();" name="frame_btnUploadImage55c3fefdb188d" style="display:none;"></iframe><input type="file" id="btnUploadImage55c3fefdb188d" name="btnUploadImage55c3fefdb188d" onchange=";ajax.submit('ClickBlocks\\Web\\UI\\POM\\ImgEditor@imgEditor55c3fefda8290-&gt;uploadImage_btnUploadImage55c3fefdb188d', 'frame_btnUploadImage55c3fefdb188d')" size="1" onmouseover=";ajax._getFormByTarget('frame_' + this.id); uploadbutton.initialize('btnUploadImage55c3fefdb188d', 0, 0);" style="position: absolute; width: 60px; top: -5000px; z-index: 1; opacity: 0; left: 445px;" runat="server">+ Add</form>

Below are what I have tried but and got error "Element is not currently visible and so may not be interacted with":

driver.findElement(By.cssSelector("input[id^='btnUploadImage']")).sendKeys("C:\a.png");

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\a.png");

driver.findElement(By.xpath("//input[text()='+ Add']")).sendKeys("C:\a.png");

Anyone has experience with this kind of button please help.

UPDATE: I found the way to do this as below:

WebElement elem = driver.findElement(By.cssSelector("*[id^=btnUploadImage"));

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("arguments[0].click();", elem);

Upvotes: 0

Views: 1116

Answers (4)

methamorf
methamorf

Reputation: 3

You can try

driver.waitUntil(ExpectedCondition.visibilityOfElementLocated(By.xpath(path)), miliseconds);

then

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\a.png");

Hope this helps!

Upvotes: 0

Khawar Iqbal
Khawar Iqbal

Reputation: 112

For such kind of error we should use thread sleep.

Thread.sleep(1000)

I hope it will work.

Upvotes: 0

dmr
dmr

Reputation: 556

Usually these input[type='file'] are invisible and to make it visible you can use javascript

# find your element 
input = driver.find_element_by_css_selector("input type="file")
# make it visible with Jquery
script = "$('input[type=\'file\']').css('top', 1)"
# or javascript
# script = "document.getElementById(\"btnUploadImage55c3fefdb188d\").style.top = 1"
driver.execute_script(script)
# after it become visible use send keys
input.send_keys(PATH_TO_FILE)

Upvotes: 0

Sandeep
Sandeep

Reputation: 76

Since element is not visible you have to add implicit wait-:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

or by adding Webdriver wait-:

new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(element));

Make sure you have triggered the event for pop up to appear.

Upvotes: 0

Related Questions