C#硒日历请点击

时间:2018-10-18 22:12:44

标签: c# selenium-webdriver xpath

希望每个人都很好。点击日历日期时遇到麻烦。

基本上,我单击一个按钮会弹出一个日历,当我检查(谷歌浏览器)日期时,以下td是我需要单击的日期;

<td class="dxeCalendarDay" savedcursor="[object Object]" style="cursor: pointer;">1</td>

我尝试了以下代码,但给了我一个错误;

FromCalendar = Chromedriver.FindElement(By.Id("ctl00_MainContent_dpStart_DDD_C_mt"));
IWebElement FromCalendar1 = Chromedriver.FindElement(By.XPath("//tr/td[contains(text(), '>1<'")); //

FromCalendar1.Click();

错误:

OpenQA.Selenium.InvalidSelectorException: 'invalid selector: Unable to locate an element with the xpath expression //ttr/td[contains(text(), '>1<' because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//ttr/td[contains(text(), '>1<'' is not a valid XPath expression.

希望有人可以帮助我。预先谢谢你。

3 个答案:

答案 0 :(得分:0)

XPath的问题在于,您试图通过包含>1<的文本来查找元素。该元素不包含>1<,而仅包含1。将其更改为//td[@class='dxeCalendarDay'][.='1'],它应该可以工作。

查看您的代码,看来您还有其他问题。

您的第一行什么都不做...我假设您要单击以打开日历?

Chromedriver.FindElement(By.Id("ctl00_MainContent_dpStart_DDD_C_mt")).Click();

通常,如果您不打算重复使用变量,请不要费心存储对Web元素的引用。更改

IWebElement FromCalendar1 = Chromedriver.FindElement(By.XPath("//tr/td[contains(text(), '>1<'")); //
FromCalendar1.Click();

公正

Chromedriver.FindElement(By.XPath("//tr/td[contains(text(), '>1<'")).Click();

由于单击元素并打开了日历,因此应假定它可能不会立即可用,因此应添加一个等待时间。您的最终代码将如下所示。

Chromedriver.FindElement(By.Id("ctl00_MainContent_dpStart_DDD_C_mt")).Click();
new WebDriverWait(Chromedriver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//td[@class='dxeCalendarDay'][.='1']"))).Click();

答案 1 :(得分:-1)

您似乎很接近。此错误消息...

// Create new empty RSAkey and import saved key from RSAkeyDAta
var RSAkey = new NodeRSA();
RSAkeyData = fs.readFileSync("input-output/RSA_private_key.txt", 'utf8');
RSAkey.importKey(RSAkeyData, 'pkcs8-public-pem');

// Decrypt input data using RSA key
var decryptedData = RSAkey.decrypt(encryptedInputData, 'json');
fs.writeFile("input-output/RSA_decrypted_" + filename, decryptedData, function(err, file) {
    if (err)
        console.log("Error:", err);
    else
        console.log("File successfully decrypted with RSA!");  
}); 

...表示您使用的 XPath 不是有效的 XPath表达式

您遇到以下几个问题:

  • OpenQA.Selenium.InvalidSelectorException: 'invalid selector: Unable to locate an element with the xpath expression //ttr/td[contains(text(), '>1<' because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//ttr/td[contains(text(), '>1<'' is not a valid XPath expression. >是标记的一部分,因此不应成为 XPath表达式的一部分。因此,您需要删除它们。
  • 您需要使用<contains(),但不能同时使用两者。
  • XPath表达式必须采用正确的格式。

因此,您自己的 XPath表达式进行了少量修改,如下所示:

text()

此外,由于要单击按钮元素以显示日历弹出窗口,因此需要诱使 WebDriverWait 使元素可点击,如下所示:

IWebElement FromCalendar1 = Chromedriver.FindElement(By.XPath("//tr/td[@class='dxeCalendarDay'][text()='1']"));
//or
IWebElement FromCalendar1 = Chromedriver.FindElement(By.XPath("//tr/td[@class='dxeCalendarDay' and contains(.,'1')]"));

注意: ChromeDriver 是保留的关键字,它不应该用作用户定义的变量

答案 2 :(得分:-1)

首先我使用了这个代码

var query = driver.FindElement(By.Id("...")); // 这将寻找您的 (id/Css/Xpath..),我创建了这个“var 查询以发送”当前数据时间 DateTime y = DateTime.Today; // 这是我的字符串 (y) 当前时间

query.SendKeys(y.ToString()); 然后我将我的字符串 (y) 发送到我的查询(我的问题是点击,所以我创建了这个代码来等待我的字符串 (y) 并点击

new WebDriverWait(driver,TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.ClassName("rcToday"))).Click(); // 好吧,当我的字符串发送 (y) 时,我的日历会显示一个 DataTime 但它不可点击,然后我在 (y) 上找到了 WebElement,在我的例子中是 (rcToday) 并且完成了我可以点击

var query = driver.FindElement(By.Id("..."));
DateTime y = DateTime.Today;
query.SendKeys(y.ToString());
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.ClassName("rcToday"))).Click();

换句话说,您可以尝试通过下面的这一步来提早一天

            driver.FindElement(By.Id**("popupButton")).Click();// look your element

            river.FindElement(By.LinkText("13")).Click();// you could try by (Id/CssSelector/Xpath etc...) to click over the day, if the element is unclikable try to put the code bellow and look for the element the day  is selected and click.

            new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.ClassName("rcWeekend"))).Click();// this class name were my element that i found.