gpt4 book ai didi

java - 无法从selenium java中的ui li元素中进行选择

转载 作者:行者123 更新时间:2023-12-01 10:33:36 24 4
gpt4 key购买 nike

我正在自动添加到“http://www.fnp.com/the-sweet-surprises-genpr-143431-e.html”网站的卡片流程。我正在使用 TestNG Structure 编写代码。我被困在任务中,我被困在“运输选项”框中。我在控制台中得到空值。下面是我的 testNG 程序

    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;
}

下面是 HTML 代码

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

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

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

/* 下面的代码 从“运输选项”下拉框中获取所有选项*/

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

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

String name=option.getText();

/下面两行代码是聚焦于选择框/

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

/*从下拉框中选择下拉项“标准交付[Rs.0]”*/

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

}
}

关于java - 无法从selenium java中的ui li元素中进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34945125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com