网页上的按钮不可点击



我曾问过一个问题,但在解析网页时仍然面临随机错误。场景是系统转到https://www.sprouts.com/store/tx/plano/plano/单击"查看本店特色商品"导航至https://shop.sprouts.com/shop/flyer并提取商店特价商品。目前,下面的代码只有10%或20%的时间有效,因为它无法找到单击并导航到下一页的按钮。我做错了什么?

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;
[TestClass]
public class UnitTest1
{
ChromeDriver driver;
WebDriverWait webDriverWait;
[TestInitialize]
public void Startup()
{
var chromeOptions = new ChromeOptions();
//chromeOptions.AddArguments("headless");
chromeOptions.AddArguments("--proxy-server='direct://'");
chromeOptions.AddArguments("--proxy-bypass-list=*");
//chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
//chromeOptions.AddArguments("--disable-extensions");
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 GetSproutsWeeklyAdDetails()
{   
try
{        driver.Navigate().GoToUrl("http://www.sprouts.com/store/tx/plano/plano/");
}
catch (TimeoutException timeoutException)
{
driver.Navigate().Refresh();
}
webDriverWait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
**var elements1 = webDriverWait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(
By.XPath("//div[@class='cell small-6 divider']/button")));
elements1.First().Click();**
<= the system is unable to find the button 80% of the times
webDriverWait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
var elements2 = webDriverWait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(
By.XPath("//li[@class='cell-wrapper' and @ng-repeat='item in items track by $index']"))); 
//More code below    
}
}
}

不可点击按钮所在区域的源代码:

<div class="cell small-6 divider">        
<button onclick="viewStoreFlyer(event, 101)">
<img src="https://www.sprouts.com/wp-content/themes/FoundationPress/dist/assets/images/weekly-specials-stores-icon.svg" width="32" alt="" role="presentation">
<br>
View this store’s specials
</button>        
</div>

在我的文章中,除了两件事之外,代码几乎都很好。您正在等待所有元素都可见,然后立即想要单击其中的第一个元素。首先,网站上只有一个元素具有以下xPath,因此无需查找列表。第二个问题是,当元素可见时,并不意味着它可以点击,所以处理这个问题的更好方法是:

webDriverWait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='cell small-6 divider']/button"))); 

PS:在这种情况下,@DebanjanB提供的答案非常好,因为您只想在不与元素交互的情况下提取文本。在我们的情况下,我们必须等到element准备好接收点击,这就是为什么ElementToBeClickable在这种情况下更好的原因。

我可以通过在点击按钮之前添加线程睡眠来实现这一点。

var elements1 = webDriverWait.Until(ExpectedConditions.ElementToBeClickable(
By.XPath("//div[@class='cell small-6 divider']/button")));
Thread.Sleep(2000);
elements1.Click();

相关内容

  • 没有找到相关文章

最新更新