Selenium's IE driver doesn't load page? - java

I try to load webpage and extract some docs, but after typing in code and pressing "search" button, nothing shows up. When i do it on my regular website, it works fine. Should I somehow set my WebDriver differently?
I tried with simple firefox driver but it also failed.
WebDriver driver = new InternetExplorerDriver();
driver.get("https://ekrs.ms.gov.pl/rdf/pd/");
WebElement krsinput = driver.findElement(By.name("unloggedForm:krs2"));
krsinput.sendKeys(String.valueOf(myNumber);
WebElement szukajbutton = driver.findElement(By.name("unloggedForm:submit"));
szukajbutton.click();
I typed in "331515" in the inputbox (there's only one) and after pressing "szukaj", there should be table showing below button.

First, there is a tip on input field: Pole KRS jest dziesięcioznakowe.
So, your code should look like
String strMyNumber = "0000331515";
WebDriver driver = new InternetExplorerDriver();
driver.get("https://ekrs.ms.gov.pl/rdf/pd/");
WebElement krsinput = driver.findElement(By.name("unloggedForm:krs2"));
krsinput.sendKeys(strMyNumber);
WebElement szukajbutton = driver.findElement(By.name("unloggedForm:submit"));
szukajbutton.click();
And your code is fully workable.
By my opinion, it's a website's issue. I have tried few correct format case numbers that I've found on some Polish websites, all of them gave no result to me.

Related

Selenium chrome driver (Java) click() login button doesn't work programmatically but works manually

I'm just doing some test automation of web UI, specifically this page https://autorefi.capitalone.com/login/
I am locating the lastname, zipcode, ssn input boxes and typing in data (the data here doesn't matter). I am then simply using the locator to click the "Sign In" button. The problem is, everytime I run this within my code (Java) using selenium/chromedriver, I get an error
Sorry, we weren't able to log you in. If you continue to see this error, make sure you're using one of our supported browsers.
The problem is this is not the correct error message. You can try this yourself by simply opening another tab and entering a random lastname, zipcode, and last 4 digit of SSN. Conversely, if you actually had an offer with Capital one, it would bring up a different page completely. The point is, the first error message I posted only comes via selenium and is not correct. The correct error message is:
Sorry, it looks like you don't have an offer with Capital one.
I tried sleeping the thread before clicking the button ,because I thought it was maybe clicking it too fast, but it still didn't work. I a bit perplexed why doing the same set of operations manually seems to work, but launching this programmatically through selenium. Can anyone provide any insight here? My code is:
WebElement element;
WebDriver driver = null;
ChromeOptions options = new ChromeOptions();
options.setPageLoadStrategy(PageLoadStrategy.NONE);
driver = new ChromeDriver(options);
WebDriverManager.getInstance(CHROME).setup();
// TODO: PROD
driver.get("https://autorefi.capitalone.com/login/");
WebElement refiCommonLoginForm = new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(By.tagName("refi-common-login-form")));
WebElement shadowRoot1 = expandRootElement(refiCommonLoginForm, driver);
WebElement refiCommonLastName = shadowRoot1.findElement(By.tagName("refi-common-last-name"));
WebElement refiCommonLastNameShadowRoot = expandRootElement(refiCommonLastName, driver);
WebElement refiCommonZip = shadowRoot1.findElement(By.tagName("refi-common-zip"));
WebElement refiCommonZipShadowRoot = expandRootElement(refiCommonZip, driver);
WebElement refiCommonLastFourSSN = shadowRoot1.findElement(By.tagName("refi-common-last-four-ssn"));
WebElement refiCommonLastFourSSNShadowRoot = expandRootElement(refiCommonLastFourSSN, driver);
refiCommonLastNameShadowRoot.findElement(By.id("loginLastName")).sendKeys("random last name");
refiCommonZipShadowRoot.findElement(By.id("loginZipCode")).sendKeys("43978");
refiCommonLastFourSSNShadowRoot.findElement(By.id("loginLastFourSSN")).sendKeys("3483");
Thread.sleep(2000);
shadowRoot1.findElement(By.tagName("button")).click();
Absolutely not sure the issue is that, but still possibly this will help.
Instead of clicking the "Sign in" button try submitting it i.e. shadowRoot1.findElement(By.tagName("button")).submit()
But I guess the issue here is that this site has some kind of anti bot defense that blocks automated access to it.

How to know webDriver opened URL successfully

How to know WebDriver opened an URL successfully after driver.get(appURL)? I can see it opens nicely in a browser. But I would like to make sure programmatically.
Hey. Here I am asking whether driver.get(appURL) returns any response code like http response. Or I have to find a ID from the web page and find it, then make conclusion, but the approach seems too primitive. I am looking for more simple solution. Someone suggested assertTrue, but some reason Eclipse is giving long error.
The simplest way to do so, would be to assert over the page title of the url you have opened :
String actualTitle = driver.getTitle();
String expectedTitle = "YourExpectedPage"; // replace with the expected page title
org.junit.Assert.assertTrue(expectedTitle.equals(actualTitle));
you can wait some time and check expected result then check current url
String currentURL = null;
WebDriverWait wait = new WebDriverWait(driver, 10);
if(driver.findElement(By.xpath("//*[#id='someID']")).isDisplayed()){ //add id or xpath
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
}
Each browser has a default error page. For chrome i am using:
if(driver.findElement(By.xpath("//div[#class='error-code']")).isDisplayed()){
MessageBox.Show("chrome error page.");
}
Try this with simple statements
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
String baseUrl="https://stackoverflow.com/";
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
if(baseUrl.equals(driver.getCurrentUrl()))
{
System.out.println("URLS are matching");
}else
{
System.out.println("URLS are not matching");
}
As a End User we really don't have to be concerned about whether WebDriver instance opened an URL successfully or not because once the WebDriver instance requests for a URL, the Browser Client on opening the webpage/website (by default) returns document.readyState as complete to the WebDriver instance and only then our next line of code gets executed.
However, as an End User we can configure the WebDriver instance to act on different available states of the DOM as well. Currently Selenium recognizes document.readyState at 3 different stages as follows:
none - The document is still loading
eager - The document is interactive
normal - The document is complete
Hence our scripts can be written to configure the WebDriver instance to respond as per your requirement.

Selenium code to open facebook messenger and send a message

I'm just starting to learn a little bit of Selenium scripting (in Java). Currently I'm trying to open a chat box in Facebook and send a message.
I have gotten up to being able to open the chat box through selenium, however I can't figure out how to actually type things into the text box.
Currently here is my code:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class practice {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:/max/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com/login/");
driver.manage().window().maximize();
driver.findElement(By.name("email")).sendKeys("myemail#yahoo.com");
driver.findElement(By.name("pass")).sendKeys("myPassword");
driver.findElement(By.id("loginbutton")).click();
driver.findElement(By.name("mercurymessages")).click();
driver.findElement(By.cssSelector("a[href*='https://www.facebook.com/messages/conversation-8148306']")).click();
// This has worked randomly. Sometimes the driver will work and open the chat box. Sometimes it will say element not found. I didn't include the full link of the conversation because apparently you don't have to. And it has worked like this in the past.
}
}
My current issue I'd like resolved is: why does this only work sometimes, and how do i find the text box area on the chat box? I did an Inspect Element and the chat box is very odd. It doesn't have anything like an id or a name, so i can't do By.id, or By.name. I can't figure out how to do it with By.cssSelector. This is what the inspect element for the text box looks like:
textarea class="uiTextareaAutogrow _552m" data-ft=".... more stuff here" onkeydown=" more stuff here" style= "more stuff here."
You have to learn Xpath and how to create Relative xpath.The xpath for the textarea
driver.findElement(By.xpath("//textarea[#class='uiTextareaAutogrow _552m']"));
Anyhow I've made few changes that Include instead of clicking on some other message.It will create a new message and send to your friend
driver.findElement(By.name("mercurymessages")).click();
//wait for 20 seconds
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("u_0_5")));
driver.findElement(By.id("u_0_5")).click();//To click on Send a New Message Link
//To enter a name into the to field
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#class='inputtext textInput']")));
WebElement friendName = driver.findElement(By.xpath("//input[#class='inputtext textInput']"));
friendName.sendKeys("Deep");//Change it with your friend name
//wait for the user list to appear
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[#class='user selected']")));
friendName.sendKeys(Keys.ENTER);
WebElement messageBox = driver.findElement(By.xpath("//textarea[#class='uiTextareaAutogrow _552m']"));
wait.until(ExpectedConditions.visibilityOf(messageBox));
messageBox.sendKeys("Hi there");
messageBox.sendKeys(Keys.ENTER);
Hi pal hope this gonna help you:
there is a method called By.className so in order to find elements without an id or a name you can use this method, the thing it's that sometimes the class name of the element it's used in other elements, so be aware of the uses in the current page, if you are lucky and the class name is unique then you can use it :)
System.setProperty("webdriver.chrome.driver", "C:/max/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com/login");
driver.manage().window().maximize();
Thread.sleep(5000);
driver.findElement(By.name("email")).sendKeys("myemail#yahoo.com");
driver.findElement(By.name("pass")).sendKeys("myPassword");
driver.findElement(By.id("loginbutton")).click();
Thread.sleep(5000);
driver.findElement(By.name("mercurymessages")).click();
Thread.sleep(7000);// here you have to use ExpectedConditions in order to verify that the elemnt it´s "clickable"
driver.findElement(By.cssSelector("a[href*='https://www.facebook.com/messages/conversation-8148306']")).click();
Thread.sleep(5000);
WebElement mssgbox = driver.findElement(By.cssSelector("textarea[class*='uiTextareaAutogrow _552m']"));
mssgbox.click();
mssgbox.sendKeys("hi");
hope this help you.
and some times elements in the pages changes so use ExpectedConditions to be sure the element its on the page
How to send message to your friend on Facebook:: Using Selenium WebDriver
// Find Username and Enter
driver.findElement(By.id("email")).sendKeys("Email");
System.out.println("Email");
// Find Password and Enter
driver.findElement(By.id("pass")).sendKeys("Password");
System.out.println("Password");
// Click Login
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[#id='u_0_q']")).click();
System.out.println("LogIn Successfull");
Thread.sleep(2000);
// Message
driver.findElement(By.xpath(".//*[#id='u_0_f']/a/div")).click();
driver.findElement(By
.xpath(".//*[#id='u_0_f']/div/div[3]/div/div[1]/div/div/ul/li[2]/a/div/div[2]/div/div[2]/div/div[1]/strong/span"))
.click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#class='_1mf _1mj']//following :: div[11]")).click();
Thread.sleep(2000);
WebElement sendmsg = driver
.findElement(By.xpath("//div[#class='_1ia']/descendant::div[#class='_5rpu' and #role='combobox']"));
sendmsg.sendKeys("Just testing: using selenium webdriver" + Keys.ENTER);
IWebElement messageBox = driver.FindElement(By.ClassName("_1mj"));
messageBox.SendKeys(string.Format("{0} Do not try to scam people anymore!!!", iteratia++));
messageBox.SendKeys(Keys.Enter);
Using C#
I can access the message textbox to insert the message with the first line of code. The second line is inserting the message. The third line is sending it.
Result:

Cannot click element with IEDriverServer

I have an element on a web page that only becomes visible after clicking its parent element. So after clicking a demo in a list of demo's, a row of icons which represent actions for the selected demo is revealed. The following code works fine with both webdriver and chromedriver:
demo.click(); //click demo
waitForElementIsDisplayed(demoReservation_btn); //wait until reservation icon is displayed
demoReservation_btn.click(); //click icon
Originally i was getting a StaleElementReferenceException and i attempted to fix this by having a try/catch block within a while loop that would continue looping until the icon was clicked. This caused IEDriverServer to crash after a couple of loops.
I have also tried wrapping it up in an Action like so:
Action action = new Action(driver);
action.click(demo).click(demoReservation_btn).build().perform()
This results in a NoSuchElementException.
I know there are some problems mentioned in the documentation about browser focus and hovering over elements, but i dont believe this is the problem. I have tried a couple of other things like adding moverToElement to the action, hovering over the element but have had no success with these. I believe one possible solution is to use a javascript executor, but i would like to avoid this approach if possible, any other suggestions?
EDIT
IEDriverServer setup:
File file = new File("IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
return driver;
Try disabling Native events of IE
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability("nativeEvents",false);
driver = new InternetExplorerDriver(cap);
I had better result using that in C# version. Read this to learn why you may need to do this.

Test autocomplete with Selenium webdriver

I have a text box in which when I type one letter say 's' , it displays a list of results ( like google search) .
I am using latest selenium webdriver with java.
I have tried
sendKeys("s"),
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onkeyup");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onblur");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onclick");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onmouseup");
driver.findElement(By.id("assetTitle")).sendKeys(Keys.ENTER);
None of these work even after adding wait after each of the steps.
Any suggestions?
Thanks.
Update :-
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("s");
driver.findElement(By.xpath("//table[#class='gssb_m']/tbody/tr/td/div/table/tbody/tr/td/span")).click();
driver.findElement(By.name("btnG")).click();
Update 2 : -
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
WebElement query = driver.findElement(By.name("destination"));
query.sendKeys("s");
Update 3 :-
I tried with Selenium 1 and the fireevent method works by passing parameter as 'keydown'. This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");
I found a workaround about this. My problem was:
Selenium inputted 'Mandaluyong' to an auto-suggest location field
The auto-suggest field appears together with the matched option
Then selenium left the auto-suggest drop-down open not selecting the matched option.
What I did was:
driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);
This is because on a manual test, when I try to press TAB key, two things were done by the system:
Picks the matched option from the auto-suggest drop-down
Closes the auto-suggest drop-down
I believe you are testing auto-suggest here (not auto-complete)
Steps I follow -
Enter something in the input field
Click on the suggestion you want to choose. (You can find the xpath using some tools like Firebug with Firepath, Chrome, etc.)
Verify the text in the input field is same as expected.
This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");

Categories