无法从java中选择ui

无法从java中选择ui

本文介绍了无法从java中选择ui li元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将自动添加到卡片进程的网站。我正在使用TestNG结构编写代码。我陷入了困在运送选项框中的任务。
我在控制台中获得了空值。
下面是我的testNG程序

 包脚本; 

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

导入pom.Homepage_POM;

public class mis {
public static WebDriver driver;
public static void main(String a [])
{
driver = new FirefoxDriver();
driver.manage()。window()。maximize();
driver.manage()。timeouts()。implicitlyWait(30,TimeUnit.SECONDS);
driver.get(http://www.fnp.com/the-sweet-surprises-genpr-143431-e.html);

driver.findElement(By.id(tagsf2))。sendKeys(Gandhinagar);
String xp =// ul / li / a [@ class ='ui-corner-all'];
列表< WebElement> allList = driver.findElements(By.xpath(xp));
int count = allList.size();
for(int i = 0; i< count; i ++)
{
String name = allList.get(i).getText();
//System.out.println(name);
if(name.contains(Gandhinagar))
{
allList.get(i).click();
休息;
}

}
List< WebElement> allElements = driver.findElements(By.xpath(// div [@ id ='ddShipingModep1_container'] / ul / li));
for(int i = 0; i< allElements.size(); i ++)
{
String name2 = allElements.get(i).getText();
System.out.println(name2);
if(name2.contains(Standard Delivery [Rs。0]))
{
allElements.get(i).click();
休息;
}

以下是HTML代码

 < div class =textbox> 
< div id =plsenterstyle =display:none>仅适用于母亲节的标准配送< / div>
< div id =anandclass =pro_detail_form_inn flot-right>
< label> 2。运送选项< / label>
< input id =ddShipingModep1_inputclass =selectboxtype =textautocomplete =offreadonly =readonly/>
< div id =ddShipingModep1_containerclass =selectbox-wrapperstyle =display:none; width:274px;>
< ul>
< li id =ddShipingModep1_input_option1class =>选择运输模式< / li>
< li id =ddShipingModep1_input_SHMD-10class =>标准传送[Rs。 0]< / li>
< li id =ddShipingModep1_input_SHMD-768class =selected>修复时间递送[Rs。 200]< / li>
< li id =ddShipingModep1_input_SHMD-781>午夜送货[卢比。 250]< / li>
< / ul>
< / div>
< input id =valentineDisabledtype =hiddenvalue =false/>
< input id =isValentinerosetype =hiddenvalue =false/>
< / div>


解决方案

  WebDriver驱动程序= new FirefoxDriver(); 

driver.manage()。window()。maximize();
driver.manage()。timeouts()。implicitlyWait(30,TimeUnit.SECONDS);
driver.get(http://www.fnp.com/the-sweet-surprises-genpr-143431-e.html);
driver.findElement(By.id(tagsf2))。sendKeys(Gandhinagar);

/ *下面三行代码是从下拉框中选择选项Gandhinagar城市名* /

  WebElement selectShippingModeInput = driver.findElement(By.id(ddShipingModep1_input)); 
selectShippingModeInput.click();
selectShippingModeInput = driver.findElement(By.id(ddShipingModep1_input));
selectShippingModeInput.click();

/ *以下代码
从Shipping的下拉框中提取所有选项选项* /

 列表< WebElement>选项= driver.findElements(By.xpath(// DIV [@ ID = ddShipingModep1_container] / UL /锂)); 
for(WebElement option:options){

/ *下面的代码获取文本下拉项* /

  String name = option.getText(); 

/ 以下两行代码是关注选择框 /

  selectShippingModeInput = driver.findElement(By.id(ddShipingModep1_input)); 
selectShippingModeInput.click();

/ *从下拉框中选择下拉菜单项Standard Delivery [Rs。0] * /

  if(name.equals(Standard Delivery [Rs。0])){
selectShippingModeInput。点击();
option.click();
break;

}
}


I am Automating add to card process of "http://www.fnp.com/the-sweet-surprises-genpr-143431-e.html" website. I am writing a code Using TestNG Structure . I got Stuck in task where I got stuck in "Shipping Option" Box.I am getting null value in console.Below is my testNG Program

    package script;

    import java.util.Iterator;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;

    import pom.Homepage_POM;

    public class mis {
            public static WebDriver driver;
        public static void main(String a[])
        {
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                  driver.get("http://www.fnp.com/the-sweet-surprises-genpr-143431-e.html");

 driver.findElement(By.id("tagsf2")).sendKeys("Gandhinagar");
     String xp = "//ul/li/a[@class='ui-corner-all']";
                    List<WebElement> allList = driver.findElements(By.xpath(xp));
                    int count = allList.size();
                                    for (int i = 0; i < count ; i++)
                    {
                        String name = allList.get(i).getText();
                        //System.out.println(name);
                        if(name.contains("Gandhinagar"))
                        {
                            allList.get(i).click();
                            break;
                        }

                }
                    List<WebElement> allElements = driver.findElements(By.xpath("//div[@id='ddShipingModep1_container']/ul/li"));
            for (int i = 0; i < allElements.size() ; i++)
                        {
                                String name2 = allElements.get(i).getText();
                            System.out.println(name2);
                             if(name2.contains("Standard Delivery [  Rs. 0 ]"))
                                {
                                    allElements.get(i).click();
                                    break;
                                }

Below is HTML code

<div class="textbox">
<div id="plsenter" style="display:none">Only standard delivery available for Mother's Day</div>
<div id="anand" class="pro_detail_form_inn flot-right">
<label>2. Shipping option</label>
<input id="ddShipingModep1_input" class="selectbox" type="text" autocomplete="off" readonly="readonly"/>
<div id="ddShipingModep1_container" class="selectbox-wrapper" style="display: none; width: 274px;">
<ul>
<li id="ddShipingModep1_input_option1" class="">Select Shipping Mode</li>
<li id="ddShipingModep1_input_SHMD-10" class="">Standard Delivery [  Rs. 0 ]</li>
<li id="ddShipingModep1_input_SHMD-768" class="selected">Fix Time Delivery [  Rs. 200 ]</li>
<li id="ddShipingModep1_input_SHMD-781">Midnight Delivery [  Rs. 250 ]</li>
</ul>
</div>
<select id="ddShipingModep1" class="inp_pop_quick_view" onchange="changeShipmentTime()" name="Items" style="display: none;">
<input id="valentineDisabled" type="hidden" value="false"/>
<input id="isValentinerose" type="hidden" value="false"/>
</div>
解决方案
WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.fnp.com/the-sweet-surprises-genpr-143431-e.html");
driver.findElement(By.id("tagsf2")).sendKeys("Gandhinagar");

/*The below three line code is to select the option "Gandhinagar " from the drop down box for City name */

WebElement selectShippingModeInput =    driver.findElement(By.id("ddShipingModep1_input"));
selectShippingModeInput.click();
selectShippingModeInput =    driver.findElement(By.id("ddShipingModep1_input"));
selectShippingModeInput.click();

/* The below code fetches all the options from the drop down box for "Shipping option"*/

List<WebElement>     options=driver.findElements(By.xpath("//div[@id='ddShipingModep1_container']/ul/li"));
for(WebElement option:options){

/*The below code fetches the text of the drop down items */

String name=option.getText();

/The below two line code is to focus on the select box/

   selectShippingModeInput = driver.findElement(By.id("ddShipingModep1_input"));
   selectShippingModeInput.click();

/*The drop down item "Standard Delivery [Rs. 0] is selected from the drop down box */

if(name.equals("Standard Delivery [ Rs. 0 ]")){
               selectShippingModeInput.click();
               option.click();
               break;

                                            }
                       }

这篇关于无法从java中选择ui li元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:32