Trying to locate Radio Button inside iFrame but getting error as No Such Element found.
Tried switching to iFrame but still getting the same error. Not sure what am I missing. Tried couple of methods but did not get through. Not sure if my Xpath is wrong or way I use id to locate element is wrong. [Verified my Xpath in Developer tools but still getting the same error of No suh Element found]
Thank you in advance.
public class Sap_Demo {
WebDriver driver;
JavascriptExecutor jse;
public static void main(String[] args)
{
Sap_Demo demoObj = new Sap_Demo();
demoObj.invokeBrowser();
demoObj.initializeSAPFiory();
demoObj.forecastMD61();
}
public void invokeBrowser()
{
System.setProperty("webdriver.chrome.driver", "U:\\Research Paper\\Selenium\\Drivers\\Chrome\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
}
public void initializeSAPFiory()
{
try
{
Thread.sleep(1200);
driver.get("https://dijon.cob.csuchico.edu:8042/erp");
driver.findElement(By.id("USERNAME_FIELD-inner")).sendKeys("H4");
Thread.sleep(1200);
driver.findElement(By.id("PASSWORD_FIELD-inner")).sendKeys("Onsjhjsa1087");
Thread.sleep(1200);
driver.findElement(By.id("CLIENT_FIELD-inner")).clear();
Thread.sleep(1200);
driver.findElement(By.id("CLIENT_FIELD-inner")).sendKeys("485");
Thread.sleep(1200);
driver.findElement(By.xpath("//span[#class='sapMBtnContent sapMLabelBold sapUiSraDisplayBeforeLogin']")).click();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void forecastMD61()
{
try {
driver.findElement(By.id("erpsim-tcode-btn-img")).click();
Thread.sleep(1200);
driver.findElement(By.id("TCode-input-inner")).sendKeys("MD61");
Thread.sleep(1200);
driver.findElement(By.id("TCode-launchBtn-content")).click();
Thread.sleep(1200);
driver.switchTo().defaultContent();
WebElement iframe = driver.findElement(By.id("ITSFRAME1"));
driver.switchTo().frame(iframe);
/*driver.switchTo().frame(driver.findElement(By.xpath("//span[#id='M0:46:::4:2-imgSymb']")));
driver.findElement(By.xpath("//span[#id='M0:46:::4:2-imgSymb']")).sendKeys("ABC");*/
//driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
//Thread.sleep(1600);
/*driver.switchTo().frame("ITSFRAME1");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("ITSFRAME1"));*/
/*WebElement E1 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("M0:46:::4:2-imgStd")));
WebElement E1 = driver.findElement(By.xpath("//span[#id='M0:46:::4:2-imgSymb']"));
E1.click();*/
//driver.findElement(By.id("M0:46:::4:2-imgStd")).click();
//driver.findElement(By.xpath("//span[#id='M0:46:::4:2-imgStd']")).click();
//Thread.sleep(1200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
Your first xpath for the element and the id for the iframe are correct, but as the window is getting opened in a new tab, you need to switch the driver to the new tab.
So, instead of using driver.switchTo().defaultContent();
You need to use:
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
And if you want to switch to the original tab, you need to use:
driver.switchTo().window(tabs.get(0));
Related
I'm trying to test our website with selenium via Jenkins so that everyday every feature gets tested.
The problem is that Jenkins fails because he is trying to get the chromedriver with my local path but there is no local path.
The error message:
[ERROR] main(at.s2gplus.selenium.TestPromoteToAdmin) Time elapsed:
0.006 s <<< FAILURE! java.lang.IllegalStateException: The driver executable does not exist:
/opt/jenkins/workspace/buzzApi/C:/Users/mario/AppData/Local/Google/Chrome/Application/chromedriver.exe
at
at.s2gplus.selenium.TestPromoteToAdmin.main(TestPromoteToAdmin.java:36)
The code:
public class TestPromoteToAdmin {
WebDriver driver = new ChromeDriver();
#Test
public void main() {
driver.get("https://192.168.1.45:8080/");
WebDriverWait wait = new WebDriverWait(driver, 90);
driver.manage().window().maximize();
driver.findElement(By.id("details-button")).click();
driver.findElement(By.id("proceed-link")).click();
driver.findElement(By.id("home-open-sign-in")).click();
driver.findElement(By.id("login-username")).sendKeys("tester");
driver.findElement(By.id("login-password")).sendKeys("wurst01");
driver.findElement(By.id("login-action")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("left-menu-room-title-add-action"))).click();
driver.findElement(By.id("create-room-name-input")).sendKeys("Selenium Admin test");
driver.findElement(By.id("create-room-description-input")).sendKeys("Selenium Test");
driver.findElement(By.id("create-room-save")).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement roomToClick = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(), 'Selenium Admin test')]")));
js.executeScript("arguments[0].scrollIntoView();", roomToClick);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wait.until(ExpectedConditions.elementToBeClickable(roomToClick)).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#id, 'left-roomlist-settingsicon')]"))).click();
WebElement addMemberBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("room-settings-add-user")));
addMemberBtn.click();
WebElement checkBox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("label[for='990bd6a8-62af-48c5-89a5-33019483c91f'")));//("cb732433-eae9-434b-96da-b4d5a499208c"));
checkBox.click();
WebElement addBtn = driver.findElement(By.id("room-settings-add-button"));
addBtn.click();
WebElement saveUserBtn = driver.findElement(By.id("room-settings-save"));
saveUserBtn.click();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#id, 'left-roomlist-settingsicon')]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("general-menu-list-item-settings"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("room-settings-context-change-role-to-moderator"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("general-menu-list-item-settings"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("room-settings-context-change-role-to-admin"))).click();
driver.findElement(By.id("room-settings-save")).click();
driver.findElement(By.id("top-logout")).click();
driver.findElement(By.id("home-open-sign-in")).click();
driver.findElement(By.id("login-username")).sendKeys("tester01");
driver.findElement(By.id("login-password")).sendKeys("wurst02");
driver.findElement(By.id("login-action")).click();
roomToClick = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(), 'Selenium Admin test')]")));
js.executeScript("arguments[0].scrollIntoView();", roomToClick);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wait.until(ExpectedConditions.elementToBeClickable(roomToClick)).click();
WebElement roomAdmin = driver.findElement(By.xpath("//span[contains(text(), 'Selenium Admin test')]"));
String room = roomAdmin.getText();
assertTrue(room, true);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#id, 'left-roomlist-settingsicon')]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("meeting-settings-open-close-room-modal"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("meeting-settings-close-room-confirm"))).click();
}
#BeforeTest
public void beforeTest() {
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.setAcceptInsecureCerts(true);
driver = new ChromeDriver(options);
}
#AfterTest
public void afterTest() {
driver.quit();
}
}
line 36 is above driver.get("https://192.168.1.45:8080/"); but I have deleted it a long time ago
Install Selenium Grid Plugin in the Jenkins, use below code for node setup. Update the versions of selenium server standalone jar and browser versions in the below code.
java -jar selenium-server-standalone-x.xx.x.jar -
Dwebdriver.chrome.driver="C:\yourpath\chromedriver.exe" -role node -hub
http://localhost:4444/grid/register -browser "browserName=internet
explorer,version=xx,platform=WINDOWS" -browser
"browserName=chrome,version=xx,platform=WINDOWS"
Please update below line of code for starting
WebDriver driver = null;
#Test
public void main() {
System.setProperty("webdriver.chrome.driver","E:\\drivers\\ChromeDrivers\\85\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://192.168.1.45:8080/");
WebDriverWait wait = new WebDriverWait(driver, 90);
driver.manage().window().maximize(); ```
After handling Alert and waits(popup at runtime,)The page is not loading which is why cannot locate elements. (in Selenium)getting no such element: Unable to locate element:
''''public class ApplicantLoginPageTest extends TestBase{
#Test
public void init() throws Exception {
ApplicantLoginPage applicantLoginPage = PageFactory.initElements(driver, ApplicantLoginPage.class);
applicantLoginPage.getApp(prop.getProperty("urlapp"));
Thread.sleep(3000);
applicantLoginPage.login(prop.getProperty("un"));
Thread.sleep(3000);
applicantLoginPage.instruct1();
Thread.sleep(3000);
applicantLoginPage.TestClick();
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
driver.switchTo().defaultContent();
} catch (Exception e) {
}
applicantLoginPage.TestCamera();
Thread.sleep(15000);
applicantLoginPage.PreviewRecord();
Thread.sleep(15000);
applicantLoginPage.instruct2();
Thread.sleep(3000);
}
}''''
remove the following statement, and try again:
driver.switchTo().defaultContent();
I am facing issues with Selenium when navigating from one page to another.
Below is the code. If I click on following element a jQuery image will open until the next page gets loaded. But it's not navigating to next page itself. The image keeps on rotating and window will close that. Why?
I am getting a NoSuchElementFoundException
Chrome Version : 58.0.3029.110
driver.findElement(By.name("continue")).click();
#Test
public void executeLoginApp() throws InterruptedException {
driver.navigate()
.to("url with login application");
WebElement userName = driver.findElement(By.id("uname"));
WebElement pwd = driver.findElement(By.id("passwd"));
userName.sendKeys("test");
pwd.sendKeys("test");
Thread.sleep(2000);
javaScriptExecutor.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Thread.sleep(2000);
//driver.findElement(By.xpath("//input[#name='continue']")).click();
//Thread.sleep(1000);
//driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.name("continue")).click();
//signOn.click();
callYourInformation();
}
private void callYourInformation() {
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String previousURL = driver.getCurrentUrl();
ExpectedCondition e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return (d.getCurrentUrl() != previousURL);
}};
wait.until(e);}*/
// currentURL = driver.getCurrentUrl();
if (driver.getCurrentUrl().contains("yourinfo")) {
// testing yourinfo page
WebElement emailAddress = driver.findElement(By.id("emailAddress"));
emailAddress.clear();
emailAddress.sendKeys("selenium#wwwmail.com");
WebElement addressLine1 = driver.findElement(By.id("addressLine1"));
addressLine1.clear();
addressLine1.sendKeys("New york");
WebElement grossAnnualIncome = driver.findElement(By.id("grossAnnualIncome"));
grossAnnualIncome.sendKeys(String.valueOf(6000));
WebElement submit = driver.findElement(By.id("submitApplication"));
submit.click();
driver.findElement(By.id("Continue")).click();
}
else{
do other logic.
}
I tried all the ways but no luck. Commented lines are my tries but none of them redirects to that page.
I'm trying to type selenium in google and get all the title text of result in a notepad file. i want to get all available links on all the pages, till last page of search. but only 1st page's link i am getting. when i debug and run, it is working for some 10 pages.help me in this.
JAVA code:
public class weblink
{
public static void main(String[] args) throws IOException, InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "E:\\disha.shah/myWork/eclipse/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
driver.findElement(By.id("_fZl")).click();
PrintStream ps = new PrintStream(new File(("E:\\disha1.txt")));
do
{
List<WebElement> findElements = driver.findElements(By.xpath("//*[#id='rso']//h3/a"));
for (WebElement webElement : findElements)
{
System.out.println("-" + webElement.getText()); // for title
//System.out.println(webElement.getAttribute("href")); // for links
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
System.setOut(ps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
Thread.sleep(1000);
if(driver.findElement(By.linkText("Next")).isDisplayed()== true)
{
driver.findElement(By.linkText("Next")).click();
}
else
{
System.out.println("All Link is Covered");
}
}
while(driver.findElement(By.linkText("Next")).isDisplayed() );
{
//Thread.sleep(2000);
}
}
}
I've done some correction. the updated code is below.-
public static void main(String[] args) throws IOException, InterruptedException
{
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "D:/Application/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
driver.findElement(By.id("_fZl")).click();
Boolean nextButtonFlag = true;
// Create two separate file storing the result
PrintStream searchTitle = new PrintStream(new File(("D:\\Titles.txt")));
PrintStream searchLink = new PrintStream(new File(("D:\\Links.txt")));
do
{
List<WebElement> findElements = driver.findElements(By.xpath("//h3[#class='r']/a"));
for (WebElement element : findElements)
{
// Write all received links and title inn txt file
searchTitle.append(element.getText()+"\n");
searchLink.append(element.getAttribute("href")+"\n");
}
Thread.sleep(2000);
try
{
driver.findElement(By.linkText("Next")).click();
}
catch(Exception e)
{
// no more next button to navigate further link
nextButtonFlag=false;
}
Thread.sleep(2500);
}
while(nextButtonFlag);
System.out.println("Execution done");
searchTitle.close();
searchLink.close();
}
}
I am working on contacts creation page in www.salesforce.com developer login account. Anybody can create a free developer login account and access this page.
I am unable to switch between frames in Lookup window. There are two frames, one is search frame with textbox and button to search and below that is results frame for displaying search results with hyperlinks for selection. I am specifying the frames by frame name or id, but webDriver switches to search frame and does the searching but then is not able to locate the results frame with NoSuchFrameException.
If I do not switch to search frame initially but directly switch to results frame, it is indeed successful in the locating the results frame which displays results of some recent searches done by default.
How do I switch from search frame to results frame ? Given below is my code
public class Acc_Parent
{
WebDriver driver;
FileInputStream fis;
XSSFWorkbook wb;
XSSFSheet sh;
#Test
public void createParent() throws IOException, InterruptedException, FileNotFoundException
{
driver = new FirefoxDriver();
//System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");
//driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("https://test.salesforce.com");
fis = new FileInputStream("C:/Users/psit/Documents/Login.xlsx");
wb = new XSSFWorkbook(fis);
sh = wb.getSheetAt(0);
driver.findElement(By.xpath(".//*[#id='username']")).sendKeys(sh.getRow(1).getCell(0).getStringCellValue());
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys(sh.getRow(1).getCell(1).getStringCellValue());
driver.findElement(By.xpath(".//*[#id='Login']")).click();
fis = new FileInputStream("C:/Users/psit/Documents/Input.xlsx");
wb = new XSSFWorkbook(fis);
sh = wb.getSheet("Parent");
driver.findElement(By.xpath(".//*[#id='Account_Tab']/a")).click();
driver.findElement(By.xpath(".//*[#id='hotlist']/table/tbody/tr/td[2]/input")).click();
driver.findElement(By.xpath(".//*[#id='j_id0:acctFrm:nmsrch']")).sendKeys(sh.getRow(1).getCell(1).getStringCellValue());
driver.findElement(By.xpath(".//*[#id='j_id0:acctFrm']/div[1]/div[2]/table/tbody/tr/td[2]/input")).click();
try
{
driver.findElement(By.linkText("click here")).click();
driver.findElement(By.xpath(".//*[#id='parentAcc']")).sendKeys(sh.getRow(1).getCell(1).getStringCellValue());
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='childAcc']")).sendKeys(sh.getRow(1).getCell(2).getStringCellValue());
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:0:actFields']")).sendKeys(sh.getRow(1).getCell(3).getStringCellValue());
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:2:actFields']")).sendKeys(sh.getRow(1).getCell(4).getStringCellValue());
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:4:actFields']")).sendKeys(sh.getRow(1).getCell(5).getStringCellValue());
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:6:actFields']")).sendKeys(sh.getRow(1).getCell(6).getStringCellValue());
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:8:actFields']")).sendKeys(Integer.toString((int)sh.getRow(1).getCell(7).getNumericCellValue()));
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:1:actFields']")).sendKeys(sh.getRow(1).getCell(8).getStringCellValue());
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:3:actFields']")).sendKeys(sh.getRow(1).getCell(9).getStringCellValue());
String mainWindow = driver.getWindowHandle();
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:5:actFields_lkwgt']/img")).click();
Thread.sleep(5000);
Set<String> winhand = driver.getWindowHandles();
for(String str : winhand)
{
if(!str.equalsIgnoreCase(mainWindow))
{
driver.switchTo().window(str);
break;
}
}
try
{
driver.switchTo().frame("searchFrame");
driver.findElement(By.xpath(".//*[#id='lksrch']")).sendKeys(sh.getRow(1).getCell(10).getStringCellValue());
driver.findElement(By.xpath(".//*[#id='theForm']/div/div[2]/input[2]")).click();
Thread.sleep(5000);
driver.switchTo().frame("resultsFrame");
driver.findElement(By.linkText(sh.getRow(1).getCell(10).getStringCellValue())).click();
driver.switchTo().window(mainWindow);
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:acctAdd:rptAddFields:7:actFields_lkwgt']/img")).click();
Thread.sleep(5000);
Set<String> winhandle = driver.getWindowHandles();
for(String str : winhandle)
{
if(!str.equalsIgnoreCase(mainWindow))
{
driver.switchTo().window(str);
break;
}
}
try
{
driver.switchTo().frame("searchFrame");
driver.findElement(By.xpath(".//*[#id='lksrch']")).sendKeys(sh.getRow(1).getCell(11).getStringCellValue());
driver.findElement(By.xpath(".//*[#id='theForm']/div/div[2]/input[2]")).click();
Thread.sleep(5000);
driver.switchTo().frame("resultsFrame");
driver.findElement(By.linkText(sh.getRow(1).getCell(11).getStringCellValue())).click();
driver.switchTo().window(mainWindow);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println();
driver.close();
driver.switchTo().window(mainWindow);
System.out.println("State/Region not Found");
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println();
driver.close();
driver.switchTo().window(mainWindow);
}
driver.findElement(By.xpath(".//*[#id='pg:acc:pb:j_id34:save']")).click();
Before switching to resultsFrame try to switch to defaultContent or your main frame and then switch to resultsFrame. The thing is webdriver searches the frame in current context. So under searchFrame it will never find the resultsFrame (As both might be under main frame).
Hope this helps.