How to choose select in Selenium java - java

I have wicket aplication, and I want to set one select value. Problem is that chrome driver returns error with RESPONSE FindElement ERROR
no such element: Unable to locate element: {"method":"css selector","selector":"#id104"}
(Session info: chrome=94.0.4606.81)
Here is my selenium java code:
System.setProperty("webdriver.chrome.driver", pathToDriver);
System.setProperty("webdriver.chrome.logfile", "C:\\app\\chromedriver.log");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\app\\GoogleChromePortable64\\App\\Chrome-bin\\chrome.exe");
WebDriver driver=new ChromeDriver(chromeOptions);
driver.navigate().to(url);
System.out.println(driver.getCurrentUrl());
WebElement userTextField = driver.findElement(By.id("username"));
userTextField.sendKeys(username);
WebElement passwordTextField = driver.findElement(By.id("password"));
passwordTextField.sendKeys(password);
WebElement okButton =driver. findElement(By.id("OKButton"));
okButton.click();
WebElement crdbTab =driver. findElement(By.linkText("CRDB"));
crdbTab.click();
(new WebDriverWait(driver, 5)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe_app4"));
WebElement uploadTab = driver.findElement(By.linkText("Upload"));
uploadTab.click();
System.out.println(driver.getCurrentUrl());
WebElement fileUploadButton = driver.findElement(By.id("id5f"));
fileUploadButton.sendKeys(filePath);
driver.findElement(By.id("idb")).click();
driver.findElement(By.className("main")).click();
Select headerDataSelect = new Select(driver.findElement(By.id("id104")));
headerDataSelect.selectByIndex(0);
Code fails on
Select headerDataSelect = new Select(driver.findElement(By.id("id104")));
Can someone give a point on how to solve this issue?
Thanks for help

Try adding a wait there to make elements loaded before accessing them.
Instead of
Select headerDataSelect = new Select(driver.findElement(By.id("id110")));
Try
wait = new WebDriverWait(driver, 30);
Select headerDataSelect = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id110"))));
Also make sure the id value id110 is unique and not dynamic here.

Related

Click the Button and get the size of table in Selenium java

I am trying to learn selenium to find elements and I could not clicking the "Edit"buttons and Im trying the get the size of the rows of the table.
Here is my code:
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/";
driver.get(baseUrl);
driver.manage().window().maximize();
TimeUnit.SECONDS.sleep(5);
List<WebElement> we = driver.findElements(By.linkText("Edit")); System.out.println(we.size());
we.get(1).click();
Here is the link that I am working:
https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/
driver = new ChromeDriver();
driver.get("https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
WebElement frame = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("demoFrame"))));
driver.switchTo().frame(frame);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[class=\"dx-link dx-link-edit\"]"))).click();
Use webdriver wait and switchTo frame, you have to switch to iframe first to interacte elements inside the iframe.
once you finish , if you want to interact with elements outside iframe then you have to switch out of it first.
driver.switchTo().defaultContent();
//rest of your code
The element is present inside an iframe and you need to switch iframe first and then you will be able to interact with element.
To overcome synchronization issue you need to induce WebDriverWait() and wait for frameToBeAvailableAndSwitchToIt() and following locator.
To get the link details induce WebDriverWait() and wait for visibilityOfAllElementsLocatedBy() and following locator.
WebDriver driver = new ChromeDriver();
String baseUrl = "https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/";
driver.get(baseUrl);
driver.manage().window().maximize();
new WebDiverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("demoFrame")));
List<WebElement> we =new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.linkText("Edit")));
System.out.println(we.size());
we.get(1).click();

Getting NoSuchElementException: no such element: Unable to locate element

I am trying to automate this from. But when I try to send a text to a text fields it is getting NoSuchElementException. I have tried webdriverwait also. But no luck. There is a popup on top of the window. I closed it and also I tried adding --disable-popup-blocking argument. But still it is not working. So anybody can tell me what I am doing wrong here? I highly appreciate that. Thank you.
System.setProperty("webdriver.chrome.driver", "chromedriver/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("chrome.switches", "--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,250)");
driver.findElement(By.cssSelector("#txt_insert2055107")).click();
driver.findElement(By.cssSelector("#txt_insert2055107")).sendKeys("Test");
driver.findElement(By.id("txt_insert2055108")).click();
driver.findElement(By.id("txt_insert2055108")).sendKeys("Test");
driver.findElement(By.id("btnsubmit")).click();
driver.close();
You cant send text on your input because you need to switch to iframe firts. Please find below working code:
https://www.frontrush.com/FR_Web_App/Player/PlayerSubmit.aspx?sid=MTA1NTc=-9M6ha/5BuDo=&ptype=recruit
code:
driver.get("https://columbiacougars.com/sb_output.aspx?frform=8&path=mbball");
driver.switchTo().frame(0);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
WebElement firstName=wait.until(ExpectedConditions.elementToBeClickable(By.id("txt_insert2055107")));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", firstName);
action.moveToElement(firstName).sendKeys("Test").perform();
WebElement lastName=wait.until(ExpectedConditions.elementToBeClickable(By.id("txt_insert2055108")));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", lastName);
lastName.sendKeys("Test");
action.moveToElement(lastName).sendKeys("Test").perform();
WebElement clickElement=wait.until(ExpectedConditions.elementToBeClickable(By.name("btnsubmit")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clickElement);
driver.close();

How many ways can we implement Drag & Drop functionality using Selenium and Java

How many ways can we implement Drag & Drop functionality using Selenium and Java?
Code attempt:
Thread.sleep(3000);
WebElement FROM = driver.findElement(By.xpath("(//div[#class='item-container flex-container-horizontal'])[1]"));
Thread.sleep(3000);
WebElement to = driver.findElement(By.xpath("//div[text()='Product Quality?']"));
Thread.sleep(3000);
Actions act=new Actions(driver);
act.dragAndDrop(FROM, to).build().perform();
Drag and Drop functionality can be implemented in multiple ways as follows:
Using dragAndDrop():
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement from = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
WebElement to = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
new Actions(driver).dragAndDrop(from, to).build().perform();
Chaining clickAndHold() and moveToElement():
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement drag = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
WebElement drop = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
new Actions(driver).clickAndHold(from).moveToElement(to).release(from).build().perform();
Video demonstration:

Selenium not click proper target in IE via moveToElement, works in Chrome

I'm using the following to navigate a menu in Selenium. It works perfectly in Chrome, however in IE, it ends up clicking the menu below my target and the submenu item becomes completely inaccessible.
// Actions not supported by FireFox's Marionette Driver, use chrome or ie.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode"));
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_INVENTOR_a"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
And here is where the drive is initialized
System.setProperty("webdriver.ie.driver", "C:\\Selenium\\IEDriverServer64.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
I would like to be able to click on the proper menu item to continue with my testing.
I managed to get a working solution by moving the cursor over from the offset. Not sure how well this will work across screens.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText(arg1));
//<span id="m7f8f3e49_ns_menu_INVENTOR_MODULE_a_tnode">Inventory</span>
System.out.println("Found the inventory text");
actions.moveToElement(menuHoverLink);
actions.moveByOffset(100, 10);
Thread.sleep(1000);
//actions.moveByOffset(45, 0);
WebElement subLink = driver.findElement(By.
id("m7f8f3e49_ns_menu_INVENTOR_MODULE_sub_changeapp_SRVITEM_a_tnode"));
actions.moveToElement(subLink, 100, 12)
.click().perform();

Not able to do mouse over operation using selenium webdriver

Below is the code
WebDriver dr= new ChromeDriver();
dr.get("http://obsessory.com/");
dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[1]/a")).click();
dr.findElement(By.id("email")).sendKeys("username#gmail.com");
dr.findElement(By.name("LoginForm[password]")).sendKeys("password");
dr.findElement(By.xpath(".//[#id='signIn']/div[2]/div[3]/div[3]/input")).click();
Actions action = new Actions(dr);
WebElement we = dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/a/span"));
action.moveToElement(we).moveToElement(dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/ul/li[1]/a"))).click().build().perform();
I wanted to click on 'my accounts' or any of those other links. Kindle tell me how to do that
#kavya
Please try this code. I think you are not able to type password in password textbox.
For password:
dr.findElement(By.xpath("(//input[#id='email'])[2]")).sendKeys("obsessory");
For Menu:
WebElement we = dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/a"));
WebElement ve = dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/ul/li[1]/a"));
Actions act = new Actions(dr);
act.moveToElement(we).click(ve).perform();
Hope this will work
Try with
Actions action= new Actions(dr);
WebElement we = dr.findElement(By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/a/span"));
action.moveToElement(we).perform();
By locator = By.xpath("html/body/header/div[2]/div[1]/ul/li[4]/ul/li[1]/a");
dr.click(locator);

Categories

Resources