getText() and getFirstSelectedOption().getText() not giving the expected text - selenium-chromedriver

I am trying to display all the options of a drop down and then select one option
public class Toolsqa_SelectMenu {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://demoqa.com/selectmenu/");
WebElement e = driver.findElement(By.xpath("//select[#id='speed']"));
Select s = new Select(e);
System.out.println("First selected item : " + s.getFirstSelectedOption().getText());
List<WebElement> options = s.getOptions();
for(WebElement w : options)
{
System.out.println(" Items in drop down : " + w.getText());
}
s.selectByVisibleText("Faster");
}
}
I am getting
First selected item :
Items in drop down :
Items in drop down :
Items in drop down :
Items in drop down :
Items in drop down :
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=73.0.3683.103)
(Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.13 seconds
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'ADMIN-PC', ip: '192.168.43.181', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Expected:
First selected item : Medium
Items in drop down : Slower
Items in drop down : Slow
Items in drop down : Medium
Items in drop down : Fast
Items in drop down : Faster

Related

org.openqa.selenium.StaleElementReferenceException / webdriver timeout execption in click action chrome drier

I am getting below given exception in give code :
Getting web element using xpath, so should be present in dom, but still gets this exception.
Tried with implicit wait, and thread sleep also, didnt work.
Code:
webElements = webDriver.findElements(By.xpath(variantSelectors.get(index)));
for (WebElement webElement : webElements) {
// move to variant url
Actions action = new Actions(webDriver);
action.moveToElement(webElement).click().perform();
Thread.sleep(extractionConfig.getWaitTime());
Exception :
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: headless chrome=67.0.3396.87)
(Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.12.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'Bhagwatis-MacBook-Pro.local', ip: '10.177.68.108', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.3', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.38.552518 (183d19265345f5..., userDataDir: /var/folders/x7/mp7jd3q94wl...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.87, webStorageEnabled: true}
DOM has been altered after clicking on first element in the list. so that you need to inspect / identify again. please add the following line at end of the for each loop as given below.
webElements = webDriver.findElements(By.xpath(variantSelectors.get(index)));
in cod,
webElements = webDriver.findElements(By.xpath(variantSelectors.get(index)));
for (WebElement webElement : webElements) {
// move to variant url
Actions action = new Actions(webDriver);
action.moveToElement(webElement).click().perform();
Thread.sleep(extractionConfig.getWaitTime());
webElements = webDriver.findElements(By.xpath(variantSelectors.get(index)));
Try this code :
Actions action = new Actions(webDriver);
List<WebElement> alllinks =
webDriver.findElements(By.xpath("variantSelectors.get(index)));
for(int i=0 ; i<alllinks.size() ; i++) {
int attempts = 0;
while(attempts < 2) {
try {
List<WebElement> alllinks =
webDriver.findElements(By.xpath("variantSelectors.get(index)));
action.moveToElement(webElement).click().perform();
break;
}
catch(StaleElementReferenceException e) {
System.err.println(e.getMessage());
}
attempts++;
}

getting error when run the `protractor` using chrome

I ma trying to run the protractor using chrome but i am getting error as :
protractor config.js
[15:54:58] I/launcher - Running 1 instances of WebDriver
[15:54:59] I/local - Starting selenium standalone server...
[15:55:01] I/local - Selenium standalone server started at http://172.18.32.112:51073/wd/hub
[15:56:06] E/launcher - chrome not reachable
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.10240 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.51 seconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'INCHCMLP01756', ip: '172.18.32.112', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66'
Driver info: driver.version: ChromeDriver
how to fix this?
here is my config file :
exports.config = {
framework : "jasmine",
capabilities : {
browserName : "chrome"
},
specs : ["spec.js"]
}
Thanks in advance.
You can try this? It worked for me with chrome version 58.
exports.config = {
framework : "jasmine",
capabilities : {
browserName : "chrome",
},
directConnect: true,
chromeDriver: "C:/Path/to/chromedriver.exe",
specs : ["spec.js"]
}

Not able to select from dropdown - Selenium Webdriver

I am trying to select the value SYSTEM CLASS from the dropdown but I am unable to do so. Each time, I get a NoSuchElement exception of the ElementNotVisible exception.
Please find the HTML Code below : -
<select class="gwt-ListBox">
<option value="(Please select)">(Please select)</option>
<option value="SYSTEM CLASS">SYSTEM CLASS</option>
</select>
Also, Below is the selenium code I tried using to select from the dropdown :-
Code set 1 -
WebElement dropDown = driver.findElement(By.className("gwt-ListBox"));
dropDown.click();
Thread.sleep(10000);
WebElement dropDownOptions = driver.findElement(By.id("country-codes-menu-1"));
dropDownOptions.findElement(By.name("SYSTEM CLASS")).click();
Code Set 2 -
WebElement CustClassDropDown = driver.findElement(By.className("gwt-ListBox"));
WebElement CustClassDropDown = driver.findElement(By.xpath(".//*[#id='main']/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div[3]/div/div[2]/div/div/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td[1]/table/tbody/tr[1]/td/div/table/tbody/tr/td[4]/select"));
Select className = new Select(CustClassDropDown);
className.selectByVisibleText("SYSTEM CLASS");
Code Set 3 -
new Select(driver.findElement(By.className("gwt-ListBox"))).selectByVisibleText("SYSTEM CLASS");
Thread.sleep(1000)
Each Time I run either of the three scripts, I am getting below exception : -
FAILED: createCustomer
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 20.04 seconds
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46'
System info: host: 'ABHISHEP-IN', ip: '10.176.250.119', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version:
'1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0,
platform=WINDOWS, nativeEvents=false, acceptSslCerts=true,
webStorageEnabled=true, locationContextEnabled=true,
browserName=firefox, takesScreenshot=true, javascriptEnabled=true,
cssSelectorsEnabled=true}]
Session ID: 1f9d091d-b977-4ebe-8ccd-efc82d10033c
Request you to kindly suggest how to select the values from the dropdown. Thanks.
It's hard to tell why it's not visible. You may need to wait for it to become visible or it may be that it's a hidden SELECT backing a fake dropdown. Try this...
WebElement select = new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.gwt-ListBox")));
new Select(select).selectByVisibleText("SYSTEM CLASS");
How about this..
Select cmb = new Select(driver.findElement(By.ClassName("gwt-ListBox")));
Then
cmb.selectByValue("SYSTEM CLASS");
OR
cmb.selectByIndex(1);
Try the Following:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//select[#class='gwt-ListBox']"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//option[#value='SYSTEM CLASS']"))).click();

TestNG Execution stops executing, when exception occurs, for NoSuchElement Found

Running a TestNG testcase in Eclipse, and getting the values from Ui, using attribute("value").
BUt while execution, if the webelement is not present/Found on UI, I need to take the UIvalue = 00:00.
try {
UIValue = driver.findElement(By.id("ctl00_MainContent_AllowanceGridView_SRow"+cellRosterPerioddd+cellRosterPeriodmm+"_"+PayBucket)).getAttribute("value");
// UIValue = WebelementforUIvalue;
}
catch (NoSuchElementException e) {
UIValue ="00.00";
}
But, When webelement is not Found, the execution stops, I do not want the execution to be stoppedm for the exceptions ?
#Stijn
The execution runs, when the WebElements are found on UI, but it thorws 'No such Element' and stops execution, if the web element is not present on UI, in such cases, need to take the value as '00:00'
# niharika_neo, Here is the Stack Trace below
{code}
Excel PayBucket:JRM1
Excel PayBucket Value:03:40
UI PayBucket:[[ChromeDriver: chrome on XP (0a57a30b08559c4aa0fe4124c5950760)] -> id: ctl00_MainContent_AllowanceGridView_SRow2812_JRM1]
03:52
Failed
Excel PayBucket:VOTPY
Excel PayBucket Value:02:01
UI PayBucket:[[ChromeDriver: chrome on XP (0a57a30b08559c4aa0fe4124c5950760)] -> id: ctl00_MainContent_AllowanceGridView_SRow2102_VOTPY]
03:52
Failed
Excel PayBucket:VOTFA
Excel PayBucket Value:02:00
no such element
(Session info: chrome=39.0.2171.71)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 25 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:03'
System info: host: 'rainmaker2011-5', ip: '192.168.8.70', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0'
Session ID: 0a57a30b08559c4aa0fe4124c5950760
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\LAKSHM~1.PAL\AppData\Local\Temp\scoped_dir14544_10752}, rotatable=false, locationContextEnabled=true, version=39.0.2171.71, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]
PASSED: IntegrationTests
{code}

version info not a dictionary : chromeDriver : chrome 21

I am trying to run a test using webdriver on chrome. I am getting this error message.
org.openqa.selenium.WebDriverException: unknown error: version info not a dictionary
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 16.84 seconds
Build info: version: '2.31.0', revision: '1bd294d', time: '2013-02-27 20:53:56'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_23'
Driver info: org.openqa.selenium.chrome.ChromeDriver
The version details are :
Chrome 21.0.1180.60
selenium-server-standalone-2.33.0
ChromeDriver 22,29,0.8 (I've tried with all three versions and getting errors).
My code is as below:
public class Testing {
public static void main(String args[]) throws Exception {
try {
System.setProperty("webdriver.chrome.driver",
"E://eclipse_workspace//testSelPro//lib//chromedriver.exe");
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(
new File(
"E://eclipse_workspace//testSelPro//lib//chromedriver.exe"))
.usingAnyFreePort().build();
service.start();
DesiredCapabilities capability = DesiredCapabilities.chrome();
WebDriver driver = new ChromeDriver(service, capability);
driver.get("http://www.google.com");
driver.close();
} catch (Exception e) {
System.out.println(e);
}
}
I have a limitation of running the scripts on Chrome 21 only as application is required to be certified on the same version. Also, I do not have the liberty to move to older versions of selenium as other application(s) require latest webdriver version on the framework.