-1

i am trying to get this button with selenium in c#

<a id="1|0AqnCSdkjQ0|none" href="" target="_self" rel="nofollow" class="download_link 1">Download</a>

i tried with id and class but it didn't work.

Here is the web page: http://www.mp3juices.cc/ - > on the next page

2
  • it is generating the id so i think it is not possible to get it Commented Sep 16, 2015 at 0:21
  • i tried with class "download_link 2" and i am getting this error : Compound class names are not supported. Consider searching for one class name and filtering the results. Commented Sep 16, 2015 at 0:36

4 Answers 4

1

Your code is failing with that "Compound class" error because you are, basically, asking for two class names.
The button has the class download_link.

If you do something like driver.findElements(By.className("download_link")) you'll get a List of all the buttons, and get whichever you wanted.

(The above snippet is Java, so you may have to adapt it to C#)

2
  • i did like this: ReadOnlyCollection<IWebElement> links = driver.FindElements(By.ClassName("download_link")); links[0].Click(); getting this error: Additional information: Index was out of range. Must be non-negative and less than the size of the collection means links are null Commented Sep 16, 2015 at 1:16
  • 1
    Are you sure that the button is visible at this point of execution? There may be no buttons rendered at such time. You may use new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.className("download_link")) before using the FindElements. Commented Sep 16, 2015 at 13:38
0

you can use this as a solution

driver.findElement(By.xpath("//a[@class='download_link 1'] and contains(text(),'Download')"));
2
  • Additional information: invalid selector: Unable to locate an element with the xpath expression //a[@class='download_link 1'] and contains(text(),'Download') because of the following error: Commented Sep 16, 2015 at 8:25
  • what i found out in css class name can't have space :) Commented Sep 16, 2015 at 22:52
0

Did you try

driver.FindElement(By.CssSelector("a.download_link.2")

When you tried to use "download_link 2" you were requesting two class names. You can specify two class names (or more) in a CSS Selector and put a period between them. The CSS selector above is read as find an A tag with class download_link and class 2.

-1
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        try
        {
            IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
            {
                try
                {
                    return d.FindElement(By.ClassName("dl_link 1"));
                }
                catch
                {
                    return null;
                }
            });
        }
        catch(Exception e)
        {

        }


        ReadOnlyCollection<IWebElement> lists = driver.FindElements(By.ClassName("download_link"));
        lists[0].Click();

*The code is not optimized, but it works great. (First part i am using to wait for the button to be loaded).

1
  • You should look into WebDriverWait, e.g. wait.Until(ExpectedConditions.ElementExists(someElement));
    – JeffC
    Commented Sep 17, 2015 at 4:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.