1

I am trying to scrape the text 睡觉 within the page edited out the site

It has the css selector:

#autocplt_wrap > ul:nth-child(1) > li:nth-child(1) > span:nth-child(1) > a:nth-child(1)

And x-Path:

/html/body/div[2]/div[1]/div[3]/div[2]/div[1]/div/div[1]/div[2]/div/div/ul[1]/li/span/a[1]

My code is:

IWebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
//load webdriver
driver.Manage();
driver.Navigate().GoToUrl("https://dict.naver.com/linedict/zhendict/#/cnen/home");
driver.FindElement(By.ClassName("ac_input")).SendKeys("睡觉" + Keys.Enter);
driver.FindElement(By.CssSelector("#autocplt_wrap > ul:nth-child(1) > li:nth-child(1) > span:nth-child(1) > a:nth-child(1)"));

No matter what I try I always get the error:

Unhandled exception. OpenQA.Selenium.NoSuchElementException: Unable to locate element: #autocplt_wrap > ul:nth-child(1) > 
li:nth-child(1) > span:nth-child(1) > a:nth-child(1)
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.By.<>c__DisplayClass29_0.<CssSelector>b__0(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
sourlemonaid
  • 504
  • 2
  • 6
  • 19

1 Answers1

1

The desired element is a dynamic element so to extract the text 睡觉 you have to induce WebDriverWait for the desired ElementIsVisible() and you can use either of the following Locator Strategies:

  • CssSelector:

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a.srchword[data-entryid]"))).GetAttribute("innerHTML"));
    
  • XPath:

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[@class='srchword' and @data-entryid]"))).GetAttribute("innerHTML"));
    

Using DotNetSeleniumExtras.WaitHelpers with nuget

  • CssSelector:

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.CssSelector("a.srchword[data-entryid]"))).GetAttribute("innerHTML"));
    
  • XPath:

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//a[@class='srchword' and @data-entryid]"))).GetAttribute("innerHTML"));
    

You can find a relevant discussion in WebDriverWait is not waiting for the element I specify


Reference

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • hey, thanks a lot. this did the trick... with one exception. `ExpectedConditions` is depreciated now. You need to write your own function or install `DotNetSeleniumExtras.WaitHelpers` with nuget – sourlemonaid May 31 '20 at 03:12
  • ooh forgot to mention you need to do that whole namespace thing. `SeleniumExtras.WaitHelpers.ExpectedConditions` in case someone reads this in the future – sourlemonaid May 31 '20 at 03:19
  • @sourlemonaid Updated the answer with `DotNetSeleniumExtras.WaitHelpers` – undetected Selenium May 31 '20 at 20:21