我正在尝试为https://www.booking.com/编写测试,只是为了我自己的经验。因此,我遇到了一个问题:尝试更改表单中的客人数量时,我不断收到 NoSuchElementException。
Message: OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id="xp__guests__inputs - container"]/div/div/div[2]/div/div[2]/button[2]"}
(Session info: chrome=79.0.3945.88)
我已经尝试过关于 SOF 的任何建议:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(120);
和new WebDriverWait(driver, TimeSpan.FromSeconds(200)).Until(condition: ExpectedConditions.ElementIsVisible(By.XPath("//*[@id=\"xp__guests__inputs - container\"]/div/div/div[1]/div/div[2]/button[2]")));
并尝试使用 XPath Axes 访问我需要的元素
所有这些策略似乎都不适合我。
我的代码:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
namespace Tests
{
public class Tests
{
IWebDriver driver;
[SetUp]
public void StartBrowser()
{
driver = new ChromeDriver("D:\\Development\\chromedriver_win32");
//driver.Url = "https://www.booking.com/";
}
[Test]
public void TestAccomodation()
{
driver.Url = "https://www.booking.com/";
IWebElement cityTextbox = driver.FindElement(By.XPath("//*[@id=\"ss\"]"));
cityTextbox.SendKeys("Осака");
IWebElement accomodationDate = driver.FindElement(By.XPath("//*[@id=\"frm\"]/div[1]/div[2]/div[1]/div[2]/div/div/div/div"));
accomodationDate.Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
IWebElement accArival = driver.FindElement(By.XPath("//*[@id=\"frm\"]/div[1]/div[2]/div[2]/div/div/div[3]/div[2]/table/tbody/tr[2]/td[2]"));
accArival.Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
IWebElement accDeparture = driver.FindElement(By.XPath("//*[@id=\"frm\"]/div[1]/div[2]/div[2]/div/div/div[3]/div[2]/table/tbody/tr[3]/td[2]"));
accDeparture.Click();
IWebElement partyMembers = driver.FindElement(By.XPath("//*[@id=\"frm\"]/div[1]/div[3]"));
partyMembers.Click();
//IWebElement container = driver.FindElement(By.XPath("//**[@id=\"frm\"]/div[1]/div[3]/div")); //ERROR!
IWebElement partyChildren = driver.FindElement(By.XPath("//*[@id=\"xp__guests__inputs - container\"]/div/div/div[2]/div/div[2]/button[2]")); //ERROR!
partyChildren.Click();
}
[TearDown]
public void CloseBrowser()
{
System.Threading.Thread.Sleep(5000);
driver.Close();
}
}
}
我正在测试的代码:
<div id="xp__guests__inputs-container" class="xp__guests__inputs focussable " data-group-modal="" role="region" aria-label="Число номеров и гостей" style="display: block;">
<div data-component="search/group/group" data-sb-id="main" data-sb-width="medium" data-sb-bbtool-searchbox="0" data-sb-group-children-hide="0" data-sb-group-children-ages-hide="0" data-sb-group-infants-hide="1" data-sb-group-pets-hide="0" data-sb-group-rooms-hide="0" data-sb-group-block-labels="0" data-sb-group-use_adults_label="0" data-sb-group-always-expanded="0" data-sb-group-use-bui-stepper="1" data-sb-group-bui-steppers-accessible="1" data-fe_sb_group_descriptive_dropdowns="0" data-fe_sb_universal_design="0" data-fe_sb_xpi="1" data-fe_remove_duplicate_ids="0" data-fe_sb_unique_id="" data-sb_reorder_rooms_block="1">
<div class="u-clearfix" data-render="">
<div class="sb-group__field sb-group__field-adults">
<div class="bui-stepper" data-bui-component="InputStepper">
<div class="bui-stepper__title-wrapper">
<label class="bui-stepper__title" for="group_adults">Взрослых</label>
</div>
<div class="bui-stepper__wrapper sb-group__stepper-a11y">
<input style="display: none" type="number" class="bui-stepper__input" data-bui-ref="input-stepper-field" id="group_adults" name="group_adults" min="1" max="30" value="2" data-group-adults-count="">
<button class="bui-button bui-button--secondary bui-stepper__subtract-button " data-bui-ref="input-stepper-subtract-button" type="button" aria-label="Взрослых: уменьшить количество" aria-describedby="group_adults_desc">
<span class="bui-button__text">−</span>
</button>
<span class="bui-stepper__display" data-bui-ref="input-stepper-value" aria-hidden="true">2</span>
<button class="bui-button bui-button--secondary bui-stepper__add-button " data-bui-ref="input-stepper-add-button" type="button" aria-label="Взрослых: увеличить количество" aria-describedby="group_adults_desc">
<span class="bui-button__text">+</span>
</button>
<span class="bui-u-sr-only" aria-live="assertive" id="group_adults_desc">2 Взрослых</span>
</div>
</div>
</div>
<div class="sb-group__field sb-group-children ">
<div class="bui-stepper" data-bui-component="InputStepper">
<div class="bui-stepper__title-wrapper">
<label class="bui-stepper__title" for="group_children">Детей</label>
</div>
<div class="bui-stepper__wrapper sb-group__stepper-a11y">
<input style="display: none" type="number" class="bui-stepper__input" data-bui-ref="input-stepper-field" id="group_children" name="group_children" min="0" max="10" value="0" data-group-children-count="">
<button class="bui-button bui-button--secondary bui-stepper__subtract-button sb-group__stepper-button-disabled" data-bui-ref="input-stepper-subtract-button" type="button" aria-label="Детей: уменьшить количество" aria-describedby="group_children_desc">
<span class="bui-button__text">−</span>
</button>
<span class="bui-stepper__display" data-bui-ref="input-stepper-value" aria-hidden="true">0</span>
<button class="bui-button bui-button--secondary bui-stepper__add-button " data-bui-ref="input-stepper-add-button" type="button" aria-label="Детей: увеличить количество" aria-describedby="group_children_desc">
<span class="bui-button__text">+</span>
</button>
<span class="bui-u-sr-only" aria-live="assertive" id="group_children_desc">0 Детей</span>
</div>
</div>
</div>
<div class="sb-group__field sb-group__field-rooms">
<div class="bui-stepper" data-bui-component="InputStepper">
<div class="bui-stepper__title-wrapper">
<label class="bui-stepper__title" for="no_rooms">Номера</label>
</div>
<div class="bui-stepper__wrapper sb-group__stepper-a11y">
<input style="display: none" type="number" class="bui-stepper__input" data-bui-ref="input-stepper-field" id="no_rooms" name="no_rooms" min="1" max="30" value="1" data-group-rooms-count="">
<button class="bui-button bui-button--secondary bui-stepper__subtract-button sb-group__stepper-button-disabled" data-bui-ref="input-stepper-subtract-button" type="button" aria-label="Номера: уменьшить количество" aria-describedby="no_rooms_desc">
<span class="bui-button__text">−</span>
</button>
<span class="bui-stepper__display" data-bui-ref="input-stepper-value" aria-hidden="true">1</span>
<button class="bui-button bui-button--secondary bui-stepper__add-button " data-bui-ref="input-stepper-add-button" type="button" aria-label="Номера: увеличить количество" aria-describedby="no_rooms_desc">
<span class="bui-button__text">+</span>
</button>
<span class="bui-u-sr-only" aria-live="assertive" id="no_rooms_desc">1 Номера</span>
</div>
</div>
</div>
</div>
</div>
</div>