Interrupting a file upload via Selenium using Java? - java

I'm trying to interrupt a file upload which doesn't seem possible. It seems that nothing gets executed beyond elem.sendKeys("filename.txt") until the entire file is uploaded. Further, none of the buttons are clickable although available when checked via Firebug or testing manually.
Automating the file upload and the interrupt by clicking a cancel file upload button (which is clickable when doing the test manually) leads to a test failure with the following exception: ElementNotVisibleException: Element is not currently visible and so may not be interacted with.
Is there any way I can enforce the interrupt? are there any other means that aids doing the same exact thing?. I'm using Firefox for this test.
HTML:
<div style="padding-left: 40px;">
<input id="upload" type="file" multiple="" label="File" name="upload[]" size="50">
</div>
Java:
WebElement elem = driver.findElement(By.id("upload"));
elem.sendKeys("filename.txt");
driver.navigate().refresh();
System.out.println("hi");

I have failed to see a point in doing that, but here we go:
I believe u can't stop the elem.sendKeys() method in Selenium(which does the actual upload).
Using it on a input text to set a string is the same as actually uploading a file. It's just Selenium trying to set some values on your input.
One way around would be clicking on a cancel upload button you have implemented.
Since you have tried that, this exception ElementNotVisibleException: Element is not currently visible and so may not be interacted with means exactly the that element exists (Selenium have found it on the DOM) but its not visible (the element has some properties like overflow: hidden display:none etc)
My best guess is that you should work that out and try clicking your button that implements an action of stopping the upload.

Related

Element not found when executing findElement in Selenium

I'm trying to fill in multiple forms that come after each other, all the forms get filled swiftly with no errors because I make sure to add
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
before doing anything on a new page, and I know I'm on the correct page.
On the last form, I encounter this error :
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[#id="formtovalidate"]/fieldset[1]/div/label/input For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
So I went to check on the browser by taking a screenshot and the browser is on the correct page with the correct form, I also checked the xpath values and even tried other attributes.. nothing seemed to work.
So I went ahead and printed out the PageSource which showed a totally different page (not the previous page), I also noticed the this page flashed for a second before the final form appeared.
I also tried driver.navigate().refresh() but that didn't work. I kept searching and looking but nothing appeared. I also changed browsers, that did nothing..
This is the method I'm trying to execute:
private void method() {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"formtovalidate\"]/fieldset[1]/div/label/input")));
driver.findElement(By.xpath("//*[#id=\"formtovalidate\"]/fieldset[1]/div/label/input")).sendKeys(email); }
Update
Here's the form screenshot:
Here's the execution results:
Code:
String body_text = driver.findElement(By.tagName("body")).getText();
System.out.println(body_text);
Result: The form but in text
Code:
String body_innerHTML = driver.findElement(By.tagName("body")).getAttribute("innerHTML");
System.out.println(body_innerHTML);
Result: A different page :(
<zendesk-ticketing-form base-url="https://www.runescape.com/a=870/c=K0aO9WO69EI" css-cachebust="129" sitekey="6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv" grecaptcha="" has-valid-session="true" weblogin-url="https://secure.runescape.com/m=weblogin/a=870/c=K0aO9WO69EI/loginform?mod=www&ssl=1&dest=zendesk/support-form?form=360000065898">
<div class="x-display-none ie-error-display" data-js-ie-error="">
<section class="c-article">
<div class="c-article__content">
<h1>Error: Unsupported Browser</h1>
<p>
We do not support your web browser. Please use a supported web browser by choosing one below.
<br>
FireFox
<br>
Chrome
</p>
</div>
</section>
</div>
Code:
String pagesource = driver.getPageSource();
System.out.println(pagesource);
Result: Same as the previous one.. different page..
Firefox Page Source: https://pastebin.com/Kv15V2SK
Firefox Inspect Element of the page screenshot: http://prntscr.com/qvi6hc
This is weird, as the page source is different to the form!
I couldn't find time to solve your problem. If you want to do it on your own, please Search this on Google, "Shadow Root, Selenium", I had this kind of error before. What I know is, you cannot directly reach an element that stays inside of a shadow root, This is why you are not getting the source code inside of it.
What you need to do is go through the element step by step:
You have to expand the shadow root,
Here is shadow root expand function:
public static WebElement expand_shadow_element(WebElement element)
{
WebElement shadow_root = (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
return shadow_root;
}
You can imagine this function like
.switchTo.frame()
for now..
After some researches you will understand the shadow root.
I hope I got the problem right..
Try this function, If you cannot, I will help you later on. Good Luck.
The PageSource from the <body> tag, containing...
<zendesk-ticketing-form base-url="https://www.runescape.com/a=870/c=K0aO9WO69EI" css-cachebust="129" sitekey="6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv" grecaptcha="" has-valid-session="true" weblogin-url="https://secure.runescape.com/m=weblogin/a=870/c=K0aO9WO69EI/loginform?mod=www&ssl=1&dest=zendesk/support-form?form=360000065898">
<div class="x-display-none ie-error-display" data-js-ie-error="">
<section class="c-article">
<div class="c-article__content">
<h1>Error: Unsupported Browser</h1>
<p>
We do not support your web browser. Please use a supported web browser by choosing one below.
<br>
FireFox
<br>
Chrome
</p>
</div>
</section>
</div>
...implies that the WebDriver driven Browsing Context was detected as a BOT and the navigation was blocked due to presence of reCAPTCHA.
There are different approaches to solve captcha / recaptcha. You can find a couple of relevant discussion in:
How to bypass Google captcha with Selenium
Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection
Update
From your comments now it is clear that you want to fill up the fields within the form:
At this point it is worth to mention that you had been redirected to this page for either of the following reasons:
You EmailID / UserID is banned / blocked from accessing the site.
You EmailID / UserID is black-listed from accessing the site.
As you have used a BOT to access/scrape the site which may have violated the T&C.
Solution
It would be tough to propose a solution to automatically fillup the fields as presumably the elements in the BAN APPEAL REQUEST page may be protected by Invisible reCAPTCHA and you may have to Programmatically invoke the challenge
As others have suggested, it appears RuneScape's website has detected that you're using a bot to interact with their site. It doesn't matter that you solved the captcha manually, as they can still detect automated behavior quite easily without one (and no, the navigator.webdriver flag is not their only way to detect this).
The captcha is meant to prevent automated interaction with their site, which means they don't want you using Selenium/WebDriver to interact with it. You should respect this, especially as it seems you want your account unbanned (going by the pasted snippets and screenshots), so trying to do exactly what they don't want won't win you any favors.

Having problem in file upload using selenium

I am learning to automate procress in a website. I chose http://logos.iti.gr/logos/ as a website to automate. I am facing a problem when uploading the image file using the upload an image button because this button when clicked seems to convert into a text field.
The other tutorials I followed get the id of the text field and use sendkeys to send the path because they do have separate text field and upload button key.
Here is the code that I tried :
driver.get("http://logos.iti.gr/logos/");
driver.findElement(By.id("fileToUpload")).clear();
System.out.println("Cleared");
driver.findElement(By.id("fileToUpload")).sendKeys("/home/test.jpg");
I don't know what the problem is. It just get IPDL protocol error: Handler returned error code!
org.openqa.selenium.ElementNotInteractableException: Element <input id="fileToUpload" class="input_file" name="fileToUpload" type="file"> could not be scrolled into view
Build info: version: '3.141.5', revision: 'd54ebd709a', time: '2018-11-06T11:42:16'
You get ElementNotInteractableException - it is thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with. In your case it happens because the element has style display:none. Basically, selenium (and real users as well) can't interact with non-visible elements. You need to make element visible at the frist place and then continue.
driver.get("http://logos.iti.gr/logos/");
WebElement el = driver.findElement(By.id("fileToUpload"));
System.out.println("Making element visible");
((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block';", el);
el.clear();
System.out.println("Cleared");
el.sendKeys("/home/test.jpg");
By the way here is the default element style (you can see it in browser dev tools). Pay attention to display:none. When you change the value to block don't be confused that you actually don't see any changes on a screen because element's width and height are very small.
.input_file {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
display: none;
}
Try updating your client and Firefox. Hopefully that should solve this issue.
I have never faced any issue with file uploads until the webpage mandates the dialog box while clicking the image upload. I am using latest version of ChromeDriver and Chrome.
If possible, you too switch to ChromeDriver since I find the pair working seamlessly together in my experience! :)

Java HtmlUnit download pdf file

I want to download a pdf file from a website using HtmlUnit, but I haven't been able to do it. The download is triggered by clicking this:
<form name="form" action="ADIR_24046/civil/documentos/docuN.php" method="post" target="w1">
<input type="hidden" name="dtaDoc" value="7F547EA1167820365C20BA632B62A44E0B8F37564FCB3369284927C9763DE47F23DF398C061062F1">
<i class="fa fa-file-pdf-o fa-lg" aria-hidden="true" style="color:#ab5659; cursor:pointer;" onclick="$(this).closest("form").submit();"></i>
</form>
So far every time I try to do it, when I go to open the files, it says they are corrupt. My code for downloading the files is:
public void getFile(HtmlTableRow row, String folio) throws IOException {
HtmlPage pdfPage = (HtmlPage) frame.executeJavaScript("document.getElementById('historiaCiv').children[0].children[0].children[" +
row.getIndex() + "].children[1].children[0].children[1].children[0].closest('form').submit()").getNewPage();
ReadableByteChannel rbc = Channels.newChannel(pdfPage.getWebResponse().getContentAsStream());
FileOutputStream fos = new FileOutputStream(/* download path */, false);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
Is there any good way of doing this?
Without any more details and the real page to test i can only offer some hints for the problem solving.
Split you problem into two:
click the correct element and make sure HtmlUnit downloads the pdf
get the pdf from your program and save/analyze it
Before your start:
Make sure you have no javascript errors; maybe an error stops or breaks the processing. Use the simples (default) setup of the webclient. Change the config only to solve problems and make sure you know what you are doing. And make sure you use the latest (Snapshot) version available.
Step 1:
HtmlUnit works like a browser driven by you (your program) instead of a user clicking around. There should be normally no need to inject javascript like you did in your sample. Find the control the user usually clicks and simply call click on this. Because of ajax you might wait after the click some time to get all the async stuff done.
Use a web proxy like Charles (or enable HttpClient wire logging) to see the network traffic. Clicking the right control should lead to the pdf donwload visible in Charles.
Step 2
From you info i guess you are working with a page that does not do an ordinary pdf download on base of Html. Today there are many 'clever' javascript frameworks around doing strange things to make the download more user friendly. This implies that the download is done async and for you the result of the click operation is usually the htmlpage instead of the pdf result. If Step 1 was successful you have to get the newly opened window from the webclient and take the (pdf) content from this one.
Hope that helps, if you need more help you have to provide more details (or maybe you can try to use a more high level tool like wetator that does a lot of magic to deal with all this strange pages).

selenium web driver : Unable to locate element

Well not to mention that I'm new on using the selenium web driver, I'm trying to automatize a mailbox so I can log myself in and to send a message, the thing is that I download the firebug plugin for Mozilla, that is the browser I'm currently working with, when I'm trying to find the element for the log in and the password everything goes well, because I do it by their name.
input class="_nb-input-controller" type="text" **name="login"** autocorrect="off" autocapitalize="off" value=""
input class="_nb-input-controller" type="password" **name="passwd"** value=""
but as soon as im at the mailbox entry the identificator changes it, i go to compose link and this is the code that shows up:
a class="b-toolbar__item b-toolbar__item_compose js-toolbar-item-compose daria-action" title="Compose (w, c)" href="#compose" data-action="compose.go" data-params="toolbar=1&toolbar.button=compose"
Since I don't find any method that could help on this, a friend came and told me about the xpath which was:
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
by doing this selenium did find the element and I could click on the icon,
but again.. when I'm trying to get into the "TO" prompt so I can write inside
it is not finding the element, not by xpath, or anything is there a way or formula to get elements or translate them into a way that selenium can find them???
oh i see, i re try with the element and i noticed that the one i wanted was showing as hidden using firebug so i just try the next one up and the web driver could locate the item, it was a simple app to send an email the prompt that i wanted to locate was the 'TO' field. and thanks guys for giving me the correct way of using the Xpath property
Having locators such as :
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
is generally a bad idea. If you want to build a stable test I would suggest you stay away from such.
Now I see you have attributes like #name and #class. XPath is provides you with a way to reach these elements directly by pointing the identifiers instead of the whole path to the element.
You can try something like:
//input[#name='login']
//input[#name='password']
More info at: http://www.w3schools.com/xsl/xpath_intro.asp

No able to select particular check box using Selenium (Java)

I want to click on Store Pickup Available checkbox on following page
http://www.target.com/c/pants-shorts-baby-toddler-boys-clothing/-/N-59yk1#navigation=true&viewType=medium&sortBy=newest&isleaf=true&navigationPath=59yk1&parentCategoryId=9976007&facetedValue=/-/N-59yk1&RatingFacet=0&categoryId=139007
And the particular HTML part has
<input type="checkbox" name="facetId" id="in store, onlineCheckbox3"
value="10058540" omniture="Store Pickup Eligible">
I tried many thing By.id() , By.cssSelector() and xpath also.
Can someone try and tell me the working code ... in-between I will continue trying.
The problem is the checkbox you want to click is hidden initially. You can click it with something like this:
driver.findElement(By.xpath("//a[contains(text(),'in store, online')]")).click();
driver.findElement(By.xpath("//span[contains(text(),'Store Pickup Eligible')]/../../input")).click();
This will expand the element "in store, online", then click the checkbox labelled "Store Pickup Eligible".
It will select By.id which you need to write
<input type="checkbox" name="facetId" id="facetId"
value="10058540" omniture="Store Pickup Eligible">
You can find this checkbox by CSS using: [id*='onlineCheckbox3']. As Richard stated, you must click the category to show the element first. Find this by: [id=dimensions]>ul>li:nth-child(9) a. Sometimes clicking an a doesn't work (depending on the html structure), and you must click an element inside the anchor, swap that out for span and it should work. I prefer all CSS selectors when locating elements, but you can use whichever method you prefer.

Categories

Resources