0

I'm new to Selenium Webdriver automation using Java and still learning.

In my step definition RetailTakePayment.java class, I'm trying to find element containing specific that text appears in the search result (on a popup) but for some reason I have been unable to locate and click it. I have search around and have not been very lucky. Hence, posting my questions for some guidance.

I hope these help.

Salesforce Page Vehicle Lookup: enter image description here

Popup - Lookup Search Result: enter image description here

My HTML: HTML Inspection snapshot

Two Frames in my HTML: enter image description here

My Step Definition:

@Given("^user complete payment precheck$")
    public void user_complete_payment_precheck() throws Throwable {

        // inserting installer and preferred date
        driver.findElement(By.id("page:frm:main:jobsInfo:jobsRepeat2:0:j_id164")).sendKeys("Test");
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/table/tbody/tr/td[2]/form/div[1]/div/div/div/div[2]/div[5]/div[2]/table/tbody/tr[1]/td/table/tbody/tr/td[4]/span/span")).click();

        // searching and selecting vehicle
        // click on lookup
        driver.findElement(By.xpath("//img[@alt='Vehicle Lookup (New Window)']")).click();
        Thread.sleep(2000);

        // window switch handler    
        String parentWindowHandler = driver.getWindowHandle();
        String subWindowHandler = null;

                Set<String> handles = driver.getWindowHandles();
                Iterator<String> iterator = handles.iterator();
                while (iterator.hasNext()) {
                    subWindowHandler = iterator.next();
                }
                driver.switchTo().window(subWindowHandler);

        // switch by frame
        driver.switchTo().frame(0);

        // searching - this worked!!!
        driver.findElement(By.xpath("//form[@id='theForm']/div/div[2]/input")).sendKeys("autod2018");
        Thread.sleep(1000);
        driver.findElement(By.name("go")).click();
        Thread.sleep(2000);

        // searching for vehicle - STILL NOT WORKING....
        driver.findElement(By.xpath("//div[contains(text(),'BMW-1 SERIES-AUTOd2018')]")).click();      
        // driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/div/div[2]/table/tbody/tr[2]/th/a")).click();

        // switching back to the parent window - works!
        driver.switchTo().window(parentWindowHandler);

        // save record
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/table/tbody/tr/td[2]/form/div[1]/div/div/div/div[1]/table/tbody/tr/td[2]/input[1]")).click();


    }

I get this error message:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(text(),'BMW-1 SERIES-AUTOd2018')]"}
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 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/no_such_element.html
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:19.891Z'
System info: host: 'TR-UXB0509', ip: '10.175.139.35', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.41.578737 (49da6702b16031..., userDataDir: C:\Users\qureshiz\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:57721}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 69.0.3497.100, webStorageEnabled: true}
Session ID: 6282cc65ad89a6e8dd4fe4b996269b19
*** Element info: {Using=xpath, value=//div[contains(text(),'BMW-1 SERIES-AUTOd2018')]}
    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:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:424)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:314)
    at stepDefinitions.RetailTakePayment.user_complete_payment_precheck(RetailTakePayment.java:64)
    at ?.Given user complete payment precheck(C:/Users/qureshiz/ECLIPSE/Workspace/MultipleFeaturesAndSteps/src/test/resources/features/RetailJourney.feature:69)

Other xpath tried but all have failed:

//valid xpath-1 - from ranorex selocity - didnt work
driver.findElement(By.xpath("/html//div[@id='Vehicle__c_body']/table[@class='list']//a[@href='#']")).click();

//valid xpath-2 - custom xpath - didn't work
driver.findElement(By.xpath("//*[text()='BMW-1 SERIES-AUTOd2018']")).click();

//valid xpath-3 - from chrome - didn't work
driver.findElement(By.xpath("//*[@id=\"Vehicle__c_body\"]/table/tbody/tr[2]/th/a")).click();

//valid xpath 4 - from firefox - didn't work
driver.findElement(By.xpath("/html/body/div/div[3]/div/div/div[2]/div/div[2]/table/tbody/tr[2]/th/a")).click();
zagoo2000
  • 175
  • 1
  • 3
  • 10
  • it means xpath is not correct . can u show html inspection of the element u r trying to locate ? – Amit Jain Oct 05 '18 at 11:51
  • Your XPath is incorrect show your HTML so we can easily resolve your issue. – Dhru 'soni Oct 05 '18 at 11:53
  • Thanks both , I have added snapshot of my HTML. – zagoo2000 Oct 05 '18 at 13:28
  • You switch to a frame before type words for search: `driver.switchTo().frame(0);`, If the search result table is not in the same frame as the search form, you need to switch to correct frame which the result table insides. – yong Oct 05 '18 at 14:48
  • Hi @yong , I'm switching first because I'm searching for the word in the child frame. – zagoo2000 Oct 05 '18 at 15:36
  • Can you provide the html as a code snippet rather than a screenshot? Would make figuring out your problem easier. – anonygoose Oct 05 '18 at 15:41
  • Hi all, Sorry, I have just noticed that search results are appearing in another frame (see attached). I'm trying to switch to 'resultsFrame' and getting an error that no frame found. @anonygoose my html is really large and i'm unable to add it here. Hence, the reason why I ended up doing the screenshot. – zagoo2000 Oct 05 '18 at 17:10

2 Answers2

1

Because the search form and result table are in different frame, you need to switch to corresponding frame before interact with element inside it.

 // switch to search form frame
 driver.switchTo().frame(0);

 // enter search keywords and click go
 ...

 // switch back to topmost frame in the current window.
 // this is very important, you can't directly switch to result table frame
 // from search form frame ( because it's not includes the result table frame).
 // Most of time, we back to the top frame, then jump into other frame.
 driver.switchTo().defaultContent();

 // then switch to result table frame
 driver.switchTo().frame(<index_or_name_of_result_table_frame>);
yong
  • 13,357
  • 1
  • 16
  • 27
  • Hi Yong, you were right. Search results were in separate frame. There are two frames 'searchFrame' and 'resultsFrame'. I have to swtich to `driver.switchTo().frame(0);` or `driver.switchTo().frame("searchFrame");` before I do the search. But I have not been able to switch to results frame now for some reason. Any suggestions? – zagoo2000 Oct 12 '18 at 11:23
  • This worked perfectly! I can't vote it though. Thank you yong. // first switching to search frame `driver.switchTo().frame("searchFrame");` // then switching to topmost frame `driver.switchTo().defaultContent();` // lastly switching to results frame `driver.switchTo().frame("resultsFrame");` – zagoo2000 Oct 12 '18 at 11:47
  • If it's your expected answer, please accept as your question answer. – yong Oct 12 '18 at 14:28
0

try:

driver.findElement(By.xpath("//a[contains(text(),'AUTOd2018')]")).click();

the current code is looking for div element:

driver.findElement(By.xpath("//**div**[contains(text(),'BMW-1 SERIES-AUTOd2018')]")).click();

The html provided has a element name

<a...>BMW-1 SERIES-AUTOd2018< /a>
dank8
  • 361
  • 4
  • 20
  • I think i have tried this before, just did it again still can't locate it: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[contains(text(),'AUTOd2018')]"} – zagoo2000 Oct 05 '18 at 13:30
  • have you tested the xpath in firebug. Chrome Developer console run this: document.evaluate("//p[contains(text(),'AUTOd2018')]", document, null, XPathResult.ANY_TYPE, null).iterateNext() – dank8 Oct 05 '18 at 13:57
  • No. Unfortunately, In my test environment firefox v62 firebug is not compatible. – zagoo2000 Oct 05 '18 at 14:26
  • try firefox developer console – dank8 Oct 05 '18 at 14:34
  • I didn't get a chance to try them in firefox dev console but the one i have tested have been validated (i have just added them to the description) – zagoo2000 Oct 05 '18 at 16:26
  • Just noticed the element is inside a FRAME, Selenium gives you access to the content of one frame at any time. Two examples of Selenium Frames: https://stackoverflow.com/questions/52664902/step-definition-java-selenium-webdriver-find-element-containing-text-and-click/52666602#52666602 or https://stackoverflow.com/questions/20069737/how-to-identify-and-switch-to-the-frame-in-selenium-webdriver-when-frame-does-no – dank8 Oct 06 '18 at 00:19