Search code examples
c#seleniumxpathcss-selectorswebdriverwait

How to click on an list element within ol using Selenium and C#


I am working with C# selenium as an amateur. How can I select the country on the site below. I tried many things but I couldn't choose.

Url: " https://support.google.com/legal/contact/lr_counternotice?product=websearch "

Snapshot of the element:

enter image description here


Solution

  • To click on the <li> element with text Afghanistan you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:

    • CssSelector:

      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.sc-select[aria-label^='Country of residence']"))).Click();
      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("ol.sc-select-show li[id=':2']"))).Click();
      
    • XPath:

      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='sc-select' and starts-with(@aria-label, 'Country of residence')]"))).Click();
      new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//ol[@class='sc-select-show']//li[text()='Afghanistan']"))).Click();