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

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

在我的步骤定义 RetailTakePayment.java 类中,我试图找到包含特定文本出现在搜索结果中(在弹出窗口中)的元素,但由于某种原因,我无法找到并单击它。我四处寻找,但不是很幸运。因此,发布我的问题以获得一些指导。

我希望这些有帮助。

Salesforce 页面车辆查找:

http://img1.mukewang.com/617a45520001c27f04990247.jpg

弹出 - 查找搜索结果:

http://img.mukewang.com/617a45600001817606300300.jpg

我的 HTML:

http://img.mukewang.com/617a456e00011fb408120664.jpg

我的 HTML 中的两个框架:

http://img1.mukewang.com/617a457d0001be1e07250657.jpg

我的步骤定义:


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

    }


HUWWW
浏览 155回答 2
2回答

MM们

由于搜索表单和结果表在不同的框架中,所以在与其中的元素进行交互之前,您需要切换到相应的框架。&nbsp;// switch to search form frame&nbsp;driver.switchTo().frame(0);&nbsp;// enter search keywords and click go&nbsp;...&nbsp;// switch back to topmost frame in the current window.&nbsp;// this is very important, you can't directly switch to result table frame&nbsp;// from search form frame ( because it's not includes the result table frame).&nbsp;// Most of time, we back to the top frame, then jump into other frame.&nbsp;driver.switchTo().defaultContent();&nbsp;// then switch to result table frame&nbsp;driver.switchTo().frame(<index_or_name_of_result_table_frame>);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java