Reputation: 31
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->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
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
Reputation: 112
For such kind of error we should use thread sleep.
Thread.sleep(1000)
I hope it will work.
Upvotes: 0
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
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