What is the best way to get text? getText() is not returning the link text in google.co.in - selenium-webdriver

What is the best way to get text ? getText() is not returning the link text in google.co.in
I am trying to get the below text
हिन्दी বাংলা తెలుగు मराठी தமிழ் ગુજરાતી ಕನ್ನಡ മലയാളം ਪੰਜਾਬੀ
For this i wrote the script also,but i get the output like ????.
I don't know why it happens.
can anyone suggest me to get the text.
Below is my script
public class LanguageLinks {
public static void main(String[] args) throws InterruptedException, IOException {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in");
List<WebElement> links = driver.findElements(By.xpath("//*[#id='addlang']/a"));
System.out.println(links.size());
for(int i=0;i<links.size();i++){
System.out.println(links.get(i).getText());
}
}
}
Edit note : Added code block to include complete code.

This is nothing to do with Selenium but mismatch in character encoding in Eclipse side. Please apply the following settings:
Go to : Window -> Preferences -> General -> Workspace . In Text file encoding section: Select Other radio, Select UTF-8 from combo -> Click OK button.
After above settings is applied, please re execute the code again. You should see proper texts displayed on the screen.

Related

Selenium Web-Driver Popup

I am unable to write on this popup message using selenium.
Please feel free to help me in this case.
My Code is:-
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\gecko_driver\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.heycare.com");
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("hello world-----1");
driver.switchTo().frame(0);
driver.findElement(By.id("mobile")).sendKeys("7015273543");
System.out.println("hello world-----2");
//Driver.findElement(By.id("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
}
Error:-
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: *[name='mobile']
Code is working fine till:-
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
Unable to write on that popup form that comes after clicking on the login button..
I navigated to that website and successfully targetted the mobile number field using this css selector #mobile which targets the id attribute. You're doing this for your password field. Try having your test use this:
Driver.findElement(By.id("mobile")).sendKeys("7015273543");
See if that helps...
Edit: I'm not sure that the unformatted code block you're showing that switches frames is necessary. Unless you're working with iframes, I didn't see any in the minute I was looking around.
Edit2:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.heycare.com");
System.out.println("hello world");
driver.findElement(By.className("log-pop")).click();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.name("mobile")));
driver.findElement(By.name("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
You could try something like this, I added in a call to WebDriverWait to account for the possible delay between clicking the login button, and waiting for the login prompt popup to finish it's animation prior to be allowed to be clicked. I'm not sure if this will work in your case, but it's a possibility I sniffed out when I was observing the site's behavior.
(I didn't test any of this code, it's freehand written. So take it with a grain of salt. It might not work flawlessly as is.
Bit of java nit, you should make your object references lowercase, save capitalized symbols for class names. I had a moment of trying to figure out why you were calling static methods of a class called Driver instead of an instance of the class WebDriver

Unable to click on google's search result through selenium web driver (Java)

I am trying to search some thing on google, lets say "Testing". So I want to click on 1st search result, but unable to do that. Please find my code below. I have tried with xpath, class and cssSelector. Please help.:
/**
* #param args
* #throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("Testing" + Keys.ENTER);
Thread.sleep(5);
driver.findElement(
By.className("r"))
.click();
}
Please find the Inspect Element result as well:
Software testing - Wikipedia, the free encyclopedia
You should try using WebDriverWait to wait until element is visible and enable to click as below :-
driver.get("https://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("Testing" + Keys.ENTER);
new WebDriverWait(driver, 10)
.until(ExpectedConditions.elementToBeClickable(By
.linkText("Software testing - Wikipedia, the free encyclopedia"))).click();
Try once giving location of link instead of header
xpath= //h3[#class='r']/a
if you are going to click on first link only, then above path work.
If you want to click on specific element, then try to build xpath which return specific element. For example to click on wikipedia link
xapth=//a[#href='https://en.wikipedia.org/wiki/Software_testing']
You can also try like
1.collecting all links into List and
2. iterating one by one
3. By using getText get text and verify required one
4. once condition satisfy then click on it.

Code is not getting executable after opening pop up in selenium

Please find below the code :
d.findElement(By.id("txtUserName")).clear();
d.findElement(By.id("txtUserName")).sendKeys("polarissupport#polaristest.net");
Thread.sleep(2000);
d.findElement(By.name("txtTempBox")).clear();
Thread.sleep(2000);
d.findElement(By.id("txtPassword")).sendKeys("password");
d.findElement(By.id("btnsubmit")).click();
Thread.sleep(2000);
d.findElement(By.id("ctl00_ContentPlaceHolder1_rdoAdmin")).click();
Thread.sleep(2000);
d.findElement(By.id("ctl00_ContentPlaceHolder1_lnkContinue")).click();
Thread.sleep(5000);
//GC Inbox
String s1 = "CHR-665";//d.findElement(By.id("ctl00_ContentPlaceHolder1_lblrequestidtext")).getText(); //For taking Request Id
String tem[] = s1.split("-");
d.findElement(By.id("ctl00_ContentPlaceHolder1_ctlCommonInbox_txtGrantID")).sendKeys(tem[1]);
d.findElement(By.id("ctl00_ContentPlaceHolder1_ctlCommonInbox_lnkSearch")).click();
Thread.sleep(5000);
d.findElement(By.id("ctl00_ContentPlaceHolder1_ctlCommonInbox_grdAdminGrantCHR_ctl00_ctl04_btnAction")).click();
Thread.sleep(8000);
d.findElement(By.xpath("//*[#id='ctl00_ContentPlaceHolder1_lnkReleasePayment']")).click();
After the last line which opens the Pop-up, code is not getting executable to handle this opened Pop-up.
Please find below the code through which I handled the Pop-up:
String MainWindow=d.getWindowHandle();
Set<String> set=d.getWindowHandles();
Iterator<String> it=set.iterator();
while(it.hasNext())
{
System.out.println("Inside While");
String ChildWindow=it.next();
if(!MainWindow.equalsIgnoreCase(ChildWindow))
{
d.switchTo().window(ChildWindow);
d.close();
}
}
System.out.println("Outside While");
d.switchTo().window(MainWindow);
Please let me know how to execute Pop-up handling code when Pop-up is opened because when I close the Pop-up manually then only this Pop-up handling code executes not at the time when Pop-up is opened.
Please let me know if question doesn't provide satisfactory information.
Thanks
Try Using Alert class or PopupWindowFinder Code in the link is based on c#, i hope you can convert it to your desired language easily
popup's in selenium webdrivers

Not able to send values to textbox after popup closed

I have a TextBox where in I need to send some numbers after selecting some value in the popup. Here, the popup will close after I select some value and click on OK button.
I am getting Element not clickable point error when I run the script.
I have tried following workarounds but nothing worked for me.
1) putting explicit wait for some seconds.
2) tried using java script executor
Please help.
Just an example .You may want to try Selenium.Interactions
public class TestPopUpElement{
public static Actions action;
public void TestPopUp(){
action = new Actions(driver);
IWebElement ElementToStore= driver.FindElement(By.XPath
("XPath of the Element"));
action.MoveToElement(Lightbox).Click().Perform();
}
}

Below code is not handelling new window in selenium webdriver

To handle multiple windows I have written below code. Its not giving me any error, however no action is getting performed on new window. Can you please help me to fix this issue or suggest me any other way. Thanks!
Code:
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
FirefoxDriver dw2 = new FirefoxDriver();
dw2.get("http://quercus0638v.quercus.kpn.org:90");
//OPEN login and provide credentials
dw2.manage().window().maximize();
Thread.sleep(2000);
dw2.findElement(By.id("ContentPlaceHolder1_GebruikersnaamTextBox")).clear();
dw2.findElement(By.id("ContentPlaceHolder1_GebruikersnaamTextBox")).sendKeys("behdse");
dw2.findElement(By.id("ContentPlaceHolder1_WachtwoordTextBox")).clear();
dw2.findElement(By.id("ContentPlaceHolder1_WachtwoordTextBox")).sendKeys("fiet$hok");
//Login : Clcik on Login button
dw2.findElement(By.id("ContentPlaceHolder1_LoginButton")).click();
//Click on Select button. After that new window will open
dw2.findElement(By.id("ContentPlaceHolder1_CWDatumRadioButton")).click();
// Below code I written to get the window handles
Set<String> AllWindowHandles = dw2.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
String window2 = (String) AllWindowHandles.toArray()[1];
System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);
//Switch to window2(child window) and performing actions on it.
dw2.switchTo().window(window2);
//Click the user
dw2.findElement(By.id("AfdelingGebruikersListView_RowCheckBox_8")).click();
//Click on OK button after selecting User
dw2.findElement(By.id("AfdelingGebruikersListView_OKButton")).click();
//Switch to window1(child window) and performing actions on it.
dw2.switchTo().window(window1);
//Click the order type you want to retrieve
dw2.findElement(By.id("ContentPlaceHolder1_VerwerktRadioButton")).click();
//Click for exporting the data to Excel Sheet
dw2.findElement(By.id("ContentPlaceHolder1_ExporteerNaarExcelButton")).click();
}
U can switch to windows using:
for (String winHandle : objDriver.getWindowHandles()) {
objDriver.switchTo().window(winHandle);
}
This will switch to the last window opened. To get back to parent window by again using the above code. It would be better that u incorporate the code in a different method in order to reuse.
Hope this helps. Thanks.
Editing the Code as per the comment by OP :-
Since, as I suggested in comment, it might not be a new window. It must be a pop-up. So, please replace your code as below and try:
//Click on Select button. After that new window will open
dw2.findElement(By.id("ContentPlaceHolder1_CWDatumRadioButton")).click()
//Click the user
try{
WebDriverWait wait = new WebDriverWait(dw2,20);
WebElement ele = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("AfdelingGebruikersListView_RowCheckBox_8")));
ele.click();
}catch(Throwable e){
System.err.println("Error while clicking on the element. "+e.getMessage());
}
//Click on OK button after selecting User
dw2.findElement(By.id("AfdelingGebruikersListView_OKButton")).click();

Resources