【问题标题】:Correct way to click a button, using Selenium使用 Selenium 单击按钮的正确方法
【发布时间】:2018-07-18 01:37:50
【问题描述】:

我曾经问过类似的问题,但我在尝试解析网页时仍然遇到错误。场景是系统导航到https://shop.sprouts.com/shop/flyer 并希望提取与每个类别相关的每个特价商品。目前,当单击一个类别时,我会在右侧看到一个空白屏幕。 我已经尝试了以下两个选项,但我得到了相同的结果。

elementLeftHandSideMenu.Click();
iJavaScriptExecutor.ExecuteScript("arguments[0].click();", elementLeftHandSideMenu);

这是时间问题吗?我做错了什么?

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using SeleniumExtras.WaitHelpers;
using System.Threading;
using System.Collections.Generic;
using Newtonsoft.Json;

[TestClass]
public class UnitTest1
{
ChromeDriver driver;
WebDriverWait webDriverWait;
[TestInitialize]
public void Startup()
{
  var chromeOptions = new ChromeOptions();
  chromeOptions.AddArguments("--proxy-server='direct://'");
  chromeOptions.AddArguments("--proxy-bypass-list=*");
  chromeOptions.AddArguments("--start-maximized");

  var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService(path);
  driver = new ChromeDriver(chromeDriverService, chromeOptions);
  webDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
}
[TestCleanup]
public void CleanUp()
{
  driver.Quit();
}
[TestMethod]
public void GetSproutsWeeklyAdDetails2()
{
  try
  {
    driver.Navigate().GoToUrl("https://shop.sprouts.com/shop/flyer");
  }
  catch (TimeoutException timeoutException)
  {
    driver.Navigate().Refresh();
  }
  var iJavaScriptExecutor = (IJavaScriptExecutor)driver;
  webDriverWait.Until(driver1 => iJavaScriptExecutor.ExecuteScript("return document.readyState").Equals("complete"));

  var elementsLeftHandSideMenu = webDriverWait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(
    By.XPath("//ul[@class='menu sidenav']/li[@class='child']/a")));
  System.Diagnostics.Debug.WriteLine(elementsLeftHandSideMenu.Count);
  var items = new List<Item>();
  foreach (var elementLeftHandSideMenu in elementsLeftHandSideMenu)
  {
    Thread.Sleep(2000);
    try
    {
      elementLeftHandSideMenu.Click();
      //iJavaScriptExecutor.ExecuteScript("arguments[0].click();", elementLeftHandSideMenu);
    }
    catch (Exception exception)
    {
      System.Diagnostics.Debug.WriteLine(exception.ToString());
    }
    //parsing code here does not work as the RHS shows a blank page
    System.Diagnostics.Debug.WriteLine("Exit");
  }
}
}

【问题讨论】:

  • 有异常吗?

标签: c# .net selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

您的元素点击似乎没有问题。您的网站似乎有问题。即使我手动单击,它也不会第一次加载。您可以尝试将 driver.Navigate().Refresh(); 从 catch 移动到尝试自身,即在页面打开后刷新页面,或者可能在第一次单击后刷新页面。我相信它会起作用的。

try
  {
    driver.Navigate().GoToUrl("https://shop.sprouts.com/shop/flyer");
    driver.Navigate().Refresh();
  }
  catch (TimeoutException timeoutException)
  {
    driver.Navigate().Refresh();
  }

如果上面的代码不行,试试-

try
    {
      elementLeftHandSideMenu.Click();
      driver.Navigate().Refresh();
      //iJavaScriptExecutor.ExecuteScript("arguments[0].click();", elementLeftHandSideMenu);
    }

【讨论】:

  • 在 elementLeftHandSideMenu.Click() 之后添加 driver.Navigate().Refresh() 修复了原始问题,但我收到“OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attach to the page文档”错误,当它转到 foreach 循环中的第二个元素时。关于我需要在这里做些什么来解决这个问题的任何意见?
  • 可能你需要在一个for循环中找到相同的元素2-3次,一旦找到就打破循环。将整个事情放在 try catch 中并捕获 Stale 元素异常。可以参考here
猜你喜欢
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 2019-10-20
  • 1970-01-01
相关资源
最近更新 更多