Selenium is showing element not found exception - selenium

I am able to do validation on first page using selenium. But when i am transferring to next page than it is showing above error.
Code is:-
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.47";
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
driver.get(baseUrl);
selenium.open("http://192.168.1.47");
selenium.type("username","clnt123822");
selenium.type("birthday", "150347");
String name = driver.findElement(By.name("username")).getAttribute("value").toString();
String dob = driver.findElement(By.name("birthday")).getAttribute("value").toString();
System.out.println(name);
System.out.println(dob);
selenium.click("btnsubmit");
// After pressing this button it is going to next page where i am having select
html tag.At this point error is coming
Exception in thread "main" com.thoughtworks.selenium.SeleniumException: Element select1 not found
This is second page checking which is throwing error
selenium.type("select1","r");
String select1 = driver.findElement(By.id("sel1")).getAttribute("value").toString();
System.out.println(select1);

I guess you need to wait until the element in the second page to be visible/clickable. Try this code just after hitting the submit button in page one.
WebElement element = new WebDriverWait(Driver,30).until(ExpectedConditions.elementToBeClickable(By.id("sel1")));

Related

How to enter string in browser address bar by using getText() method, in selenium webdriver java

I wants to open a new tab which contains a dynamic url. The url I am getting store in str variable as shown below:
String str = WebPublish_URL.getText();
Now I wants a tab with url by using getText() method.
There are 4 ways to do this. In below examples I am doing following steps,
Launching https://google.com
Searching for facebook text and getting the facebook URL
Opening facebook within and different tab.
Solution#1: Get the URL value and load it in existing browser.
driver = new ChromeDriver(options);
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(#href,'facebook.com')])[1]")).getAttribute("href");
driver.get(facebookUrl);
Solution#2: Using window handles.
driver = new ChromeDriver(options);
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(#href,'facebook.com')])[1]")).getAttribute("href");
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get(facebookUrl);
Solution#3: By creating new driver instance. It's not recommended but it is also a possible way to do this.
driver = new ChromeDriver(options);
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(#href,'facebook.com')])[1]")).getAttribute("href");
/*Create an another instance of driver.*/
driver = new ChromeDriver(options);
driver.get(facebookUrl);
Update with Selenium 4:
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(#href,'facebook.com')])[1]")).getAttribute("href");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to(facebookUrl);
.getText is to get the text from a web element. If your str contains a URL and you want to open a new tab and then load str containing URL, try the below steps :
to open a new tab
((JavascriptExecutor) driver).executeScript("window.open()");
once it's opened up you would need to switch as well.
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
so basically, in sequence :
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get(str);

selenium is trowing an error as element is not visible

I have the java script injection to send text into a hidden inputbox. but webdriver is throwing an error as element is not visible so cannot be interacted with.
WebElement tmpElement= driver.findElement(By.className("cwd_input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript(("document.getElementsByClassName('cwd_input')
[0].click();"),tmpElement);
tmpElement.sendKeys("T");
you can set value of the input via JS.
Code will be something like:
string scriptToExecute = "document.getElementById("mytext").value = 'New value';";
executor.executeScript(scriptToExecute);

Selenium WebDriver Yahoo mail sign out error

I'm new to Selenium WebDriver and I am trying to write a simple script to log into a yahoo email account and then logout. I have managed to get the login part work working but I haven't been able to get the sign out part to work. I get the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate
element: {"method":"link text","selector":"Sign Out"}
I have tried the following 3 things so far:
1)
driver.findElement(By.linkText("Sign Out")).click();
2)
driver.findElement(By.partialLinkText("Sign Out")).click();
3)
WebElement element2 = driver.findElement(By.linkText("Sign Out"));
element2.submit();
I essentially get the same error in each case. I have pasted my code below. Any help would be appreciated.
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://login.yahoo.com/config/login_verify2?.intl=ca&.src=ym");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.id("username"));
element.sendKeys("myid#yahoo.com");
driver.findElement(By.id("passwd")).sendKeys("mypassword");
element.submit();
Thread.sleep(40000);
driver.findElement(By.linkText("Sign Out")).click();
Thread.sleep(40);
Mouse over
new Actions(driver).moveToElement(driver.findElement(By.id("yucs-profile_text"))).perform();
click Sign Out
driver.findElement(By.xpath("//a[contains(text(),'Sign Out')]")).click();
Use the script below, it will work:
Actions actions = new Actions(driver);
WebElement m1 = driver.findElement(By.linkText("Sign out"));
actions.moveToElement(m1);
actions.click().build().perform();

selenium span link li not working

I am new to selenium. I am practicing to write a test case on http://www.countdown.tfl.gov.uk. Below are the steps I followed:
a) I opened the browser to selenium Web Driver
b) Found the search text Box and enter H32 and clicked on search button to selenium.
Till this part it works fine.
Now on the page I am actually getting two records on the left side of the page under the search. I am actually trying to click on the first one i.e. "Towards Southall,Townhall" link. Nothing is happening.
Below is my code:
public class CountdownTest {
#Test
public void tflpageOpen(){
WebDriver driver = openWebDriver();
searchforBus(driver,"H32");
selectrouteDirection(driver)
}
//open the countdowntfl page
private WebDriver openWebDriver(){
WebDriver driver = WebDriverFactory.getWebDriver("FireFox");
driver.get("http://www.countdown.tfl.gov.uk");
return driver;
}
private void searchforBus(WebDriver driver,String search){
WebElement searchBox = driver.findElement(By.xpath("//input[#id='initialSearchField']"));
searchBox.sendKeys(search);
WebElement searchButton = driver.findElement(By.xpath("//button[#id='ext-gen35']"));
searchButton.click();
}
private void selectrouteDirection(WebDriver driver){
WebElement towardssouthallLink= driver.findElement(By.xpath("//span[#id='ext-gen165']']"));
((WebElement) towardssouthallLink).click();
}
}
Please help me.
Thanks.
Since you are getting NoSuchElement Exception now, you may try the following code with the usage of WebDriver explicit wait.
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement towardssouthallLink = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("(//*[#id='route-search']//li/span)[1]")));
towardssouthallLink.click();
Or WebDriver implicit wait
WebDriver driver = WebDriverFactory.getWebDriver("FireFox");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("http://www.countdown.tfl.gov.uk");
Tips:
The search results need some time to be retrieved, so please use Explicit wait or Implicit wait.
Don't use locators like span[#id='ext-gen165'], they are ExtJs auto generated.
In this case, css selector can also be used: #route-search li:nth-of-type(1) > span
You aren't calling selectrouteDirection.
You probably want:
#Test
public void tflpageOpen(){
WebDriver driver = openWebDriver();
searchforBus(driver,"H32");
selectrouteDirection(driver);
}
You also don't need to cast here:
((WebElement) towardssouthallLink).click();
It's already a WebElement anyway.
I found that id for those links are dynamically generated. ids are of the form 'ext-genXXX' where XXX is number which is dynamically generated hence varies each time.
Actually, you should try with linkText:
For 'Towards Southall, Town Hall'
driver.findElement(By.linkText("Towards Southall, Town Hall")).click
For 'Towards Hounslow, Bus Station'
driver.findElement(By.linkText("Towards Hounslow, Bus Station")).click
Here is a logic:
Get all elements which have id starts with 'ext-gen' & iterate over it & click on link with matching text. Following is Ruby code(sorry, I don't know Java well):
links = driver.find_elements(:xpath, "//span[starts-with(#id, 'ext-gen')]")
links.each do |link|
if link.text == "Towards Southall, Town Hall"
link.click
break
end
end

How to locate elements on a javascript form using Selenium WebDriver

I have a page that renders a login form. Using Firefox's firebug I have identified the username id = txtUsername and the password text box is txtPassword. Everything is fine in Firefox. BUT moving over to IE Explorer browser, am getting error and cant locate the elements.
On inspecting the page, I found the form is being rendered by a JavaScript function. Below is the html:
form name="frmLogin" id="frmLogin" onsubmit="return $('#frmLogon').valid();" action="default.aspx" method="post" jQuery172043="1" novalidate="novalidate"
How can I locate the page elements using selenium webdriver.
This is the code I have written.
/* setup for IE. Remove if not needed */
File file = new File("C:/SeleniumJavaLibrary/selenium-2.28.0/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting", true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.manage().timeouts().implicitlyWait(28,TimeUnit.SECONDS );
/*setup for IE*/
driver.get("http://website.com");
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#frmLogin').valid();");
element.findElement(By.name("txtUsername")).sendKeys("user");
element.findElement(By.name("txtPassword")).sendKeys("password");
I would do the following:
WebDriverWait waiting = new WebDriverWait(driver, 15, 100);
WebElement element = waiting.until(ExpectedConditions.visibilityOfElementLocated(By.id("frmLogin")));
This will create a WebDriverWait object that will wait up to 15 seconds, checking to see if the expected condition is valid every 100 milliseconds. This object is then used to wait until the frmLogin element is visible on the page, and then returns you a WebElement that can be used later on in your test (if required).