简体   繁体   中英

css Selector for selecting specific row and column in table in selenium webdriver

When I try to find

css=table#Salarytable.dataTable.3.4 

in selenium ide, the cell is getting highlighted correctly. But when I use this locator in my selenium code..

String salary=driver.findElement(By.cssSelector("table#Salarytable.dataTable.3.4")).getText();

it is giving me error as illegal string present. EDIT: The html code is as follows:

<table id="Salarytable" class="dataTable" cellspacing="0" border="0" style="border-collapse:collapse;">

The stacktrace is:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at FunctionLibrary.FrameworkFunctions.runDriverIteration(FrameworkFunctions.java:1959)
    at FunctionLibrary.FrameworkFunctions.setActionScript(FrameworkFunctions.java:999)
    at AutomatedScripts.TableTesting.main(TableTesting.java:20)
Caused by: org.openqa.selenium.WebDriverException: An invalid or illegal string was specified
Command duration or timeout: 31 milliseconds
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 2003', os.arch: 'x86', os.version: '5.2', java.version: '1.6.0_35'
Session ID: 5d42a483-5544-4622-97aa-fc4ce00622eb
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=14.0.1, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:396)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:407)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at FunctionLibrary.FrameworkFunctions.tbl_GetCellData(FrameworkFunctions.java:3105)
    at ReusableActions.GeneralActions.BC_TableTesting(GeneralActions.java:69)
    ... 7 more
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: An invalid or illegal string was specified
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 2003', os.arch: 'x86', os.version: '5.2', java.version: '1.6.0_35'
Driver info: driver.version: unknown
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at FunctionLibrary.FrameworkFunctions.runDriverIteration(FrameworkFunctions.java:1959)
    at FunctionLibrary.FrameworkFunctions.setActionScript(FrameworkFunctions.java:999)
    at AutomatedScripts.TableTesting.main(TableTesting.java:20)
Caused by: org.openqa.selenium.WebDriverException: An invalid or illegal string was specified
Command duration or timeout: 31 milliseconds
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 2003', os.arch: 'x86', os.version: '5.2', java.version: '1.6.0_35'
Session ID: 5d42a483-5544-4622-97aa-fc4ce00622eb
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=14.0.1, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:396)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:407)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at FunctionLibrary.FrameworkFunctions.tbl_GetCellData(FrameworkFunctions.java:3105)
    at ReusableActions.GeneralActions.BC_TableTesting(GeneralActions.java:69)
    ... 7 more
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: An invalid or illegal string was specified
Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19'
System info: os.name: 'Windows 2003', os.arch: 'x86', os.version: '5.2', java.version: '1.6.0_35'
Driver info: driver.version: unknown

I know how it is done using xpath but i want to do it specifically in css. How can I do it. EDIT: I have given table#Salarytable > tbody > tr:nth-of-type(3) > td:nth-of-type(4) for now in my webdriver code and it is working fine.however i dont understand how a locator(css=table#Salarytable.dataTable.3.4) which worked in selenium ide is not working in webdriver code. Thank you.

Read about CSS classes naming rules

According to that thread CSS class name cannot start with a number, it also cannot be just a single digit. The selector you specified ( table#Salarytable.dataTable.3.4 ) implies that the HTML for table tag looks like <table id="Salarytable" class="dataTable 3 4"> This is not possible and therefore you are getting the error. Besides you don't have '3' and '4' in HTML you've provided. So the correct selector will be just table#Salarytable.dataTable .

Also it would be better to locate the table just by id:

driver.findElement(By.id("Salarytable"))

EDIT I may be wrong about naming rules. According to CSS Grammar (section G3) it's actually possible for the number to be a valid class name, however this name needs to be properly escaped when being referenced (see the linked document). Unfortunatelly my knowledge of CSS is insufficient to elaborate more on that topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM