0

I have a button control on a web page as

<button class="btn btn-mini download-attachment pull-right width100px margin-bottom-1px" type="button" data-id="48156"><i class="icon-download"></i>&nbsp;Download</button>

I want to get the data-id associated with it. I am using selenium. Please suggest how can i fetch the data-id of this button control.

JeffC
  • 22,180
  • 5
  • 32
  • 55
SA.
  • 732
  • 7
  • 20
  • 38

1 Answers1

1

To print the value of the data-id attribute you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using C# and XPath:

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(@class, 'download-attachment')][.//i[@class='icon-download']]"))).GetAttribute("value"));
    
  • Using Java and xpath:

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class, 'download-attachment')][.//i[@class='icon-download']]"))).getAttribute("data-id"));
    
  • Using Python and XPATH:

    print(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'download-attachment')][.//i[@class='icon-download']]"))).get_attribute("data-id"))
    
  • Note : For clients you have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352