我试图单击下拉菜单值以从“我的旅程http://www.makemytrip.com/”中的字段中选择城市。但是获取Stale元素引用异常。网页加载中的ID正在更改。 尝试以下代码:
driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[@id='ui-id-1']"));
driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeSelected(driver.findElement(By.xpath(".//*[@class='ui-menu-item'][2]"))));
答案 0 :(得分:0)
您可以使用以下工作代码:
const accountSid = 'AC-----------------------------';
const authToken = '---------------------------------';
const Twilio = require('twilio');
const client = new Twilio(accountSid, authToken);
client.api.calls.create({
url: 'http://demo.twilio.com/docs/voice.xml',
to: mobileNum,
from: 'fromMobileNum',
})
.then(call => {
console.log(call.sid);
});
我已修复下拉列表元素的xPath。始终尝试指定要与之交互的确切元素。例如,如果您要单击按钮,请尝试找到WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[@id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='ui-menu-item'][2]/div/p[1]/span[1]"))).click();
或<span>
标签,链接<button>
标签和输入字段<a>
标签。
答案 1 :(得分:0)
您可以尝试以下代码:
在这种情况下,我看不到 xpath 的任何使用。我已将某些 xpath 转换为 css选择器或ID。,并且只保留了一个。尽管我还没有遇到过时的元素引用。
System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("https://www.makemytrip.com/");
WebElement from = wait.until(ExpectedConditions.elementToBeClickable(By.id("hp-widget__sfrom")));
from.click();
from.clear();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul[class*='ui-widget-content hp-widget__sfrom']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(@aria-label,'Top Cities : Mumbai, India ')]"))).click();
答案 2 :(得分:0)
要单击一个下拉值,例如孟买,您可以使用以下解决方案:
代码块:
driver.get("https://www.makemytrip.com/")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='input_fromto checkSpecialCharacters ui-autocomplete-input' and @id='hp-widget__sfrom']"))).click();
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//li[@class='ui-menu-item'][starts-with(@id,'ui-id-')]//span[@class='autoCompleteItem__label']")));
for (WebElement element:myList)
if(element.getText().contains("Mumbai"));
element.click();
浏览器快照:
答案 3 :(得分:0)
下面的代码对我来说很好,它也已参数化,它适用于任何输入值而无需更改xpath。在此示例中,我以孟买作为测试数据。
driver.get("https://www.makemytrip.com/");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[contains(@id,'hp-widget__sfrom')]")).clear();
driver.findElement(By.xpath("//input[contains(@id,'hp-widget__sfrom')]")).click();
driver.findElement(By.xpath("//input[contains(@id,'hp-widget__sfrom')]")).sendKeys("Mumbai");
Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 30);
By option = By.xpath("//div[@class='autoCompleteItem']/p/span[contains(text(),'Mumbai')]");
wait.until(ExpectedConditions.elementToBeClickable(option));
driver.findElement(option).click();