当前位置: 首页 > 知识库问答 >
问题:

步骤定义Java-Selenium WebDriver从搜索结果查找元素

马承
2023-03-14

我是使用Java的Selenium Webdriver自动化的新手,并且还在学习中。

在我的步骤定义RetailTakePayment.java类中,我试图查找包含文本出现在搜索结果(弹出窗口)中的特定元素,但由于某种原因我无法找到并单击它。我到处找了找,运气不太好。因此,张贴我的问题,以寻求一些指导。

希望这些能帮上忙。

@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();


    }
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)
//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();

共有1个答案

吕琪
2023-03-14

因为搜索表单和结果表在不同的框架中,所以需要切换到相应的框架,然后再与框架中的元素进行交互。

 // 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>);
 类似资料:
  • 我试图用一个词作为查询来查找匹配项,但它似乎找不到。我正在使用标准分析仪,但找不到准确的结果。 我的索引文档是: 如果使用通配符查询: 它会返回内容:“敏捷的棕色狐狸跳过懒惰的狗” 如果使用术语查询: 它会返回内容:“敏捷的棕色狐狸跳过懒惰的狗” 现在,我想用“狐狸”作为我的新名词 但是我不知道哪个是正确的查询,或者如何做。我已经尝试了QueryParser、TermQuery和MultiPhra

  • 比对结构后,树视图显示源和目标数据库和/或模式之间的不同之处。在默认情况下,树视图内的所有对象均已勾选。你可以取消勾选你不想应用于目标的对象。你可以展开表对象以查看详细的结构。 你可以选择 “按对象类型分组”或“按操作分组”来以对象类型或作業为树视图内的对象进行分组。 操作 描述 对象存在于源和目标数据库或模式中,但具有不同的定义。目标对象将根据源对象进行修改。 对象不存在于目标数据库或模式中。将

  • 比对结构后,树视图显示源和目标数据库和/或模式之间的不同之处。在默认情况下,树视图内的所有对象均已勾选。你可以取消勾选你不想应用于目标的对象。你可以展开表对象以查看详细的结构。 你可以选择 “按对象类型分组”或“按操作分组”来以对象类型或操作为树视图内的对象进行分组。 操作 描述 对象存在于源和目标数据库或模式中,但具有不同的定义。目标对象将根据源对象进行修改。 对象不存在于目标数据库或模式中。将

  • 比对结构后,树视图显示源和目标数据库和/或模式之间的不同之处。在默认情况下,树视图内的所有对象均已勾选。你可以取消勾选你不想应用于目标的对象。你可以展开表对象以查看详细的结构。 你可以选择 “按对象类型分组”或“按操作分组”来以对象类型或作業为树视图内的对象进行分组。 操作 描述 对象存在于源和目标数据库或模式中,但具有不同的定义。目标对象将根据源对象进行修改。 对象不存在于目标数据库或模式中。将

  • 我一直在建立一个自定义搜索。该搜索具有许多下拉列表,用户可以将其留空,也可以从许多选项中进行选择。此数据保存在“wine note”自定义帖子类型的帖子中的自定义字段中。 到目前为止,它在某种程度上起了作用。我的搜索结果页面如下所示: 问题是,即使下拉选择器为空,搜索也会排除没有该特定自定义字段的任何帖子。 仅显示所有自定义字段(数据或非数据)的帖子。 因此,我稍微修改了我的代码,添加了一些if语

  • 我们在Liferay DXP和Elasticsearch 2.2.0中有一个自定义搜索portlet。我们在elasticsearch设置中为同义词搜索添加了以下设置。 我们还使用以下代码验证是否将同义词分析器添加到索引中。 这给出了synonyms.txt文件中“acl”的所有同义词的结果。但是Liferay搜索不会给搜索匹配同义词。例如:搜索(“acl”)=