Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an element where data-tb-test-id attribute is present instead of id using Selenium and Python

I'm trying to find an element using Selenium and I'm not getting it. Follow the HTML code:

<div aria-disabled="false"
     data-tb-test-id="DownloadCrosstab-Button"
     role="button"
     tabindex="0"
     style="font-size: 12px; font-weight: normal; color: rgba(0, 0, 0, 0.7); display: inline-block; padding: 0px 24px; position: relative; text-align: center; border-style: solid; border-width: 1px; border-radius: 1px; height: 24px; line-height: 22px; min-width: 90px; box-sizing: border-box; outline: none; white-space: nowrap; user-select: none; cursor: default; background-color: rgba(0, 0, 0, 0); border-color: rgb(203, 203, 203); margin-top: 8px; width: 100%; -webkit-tap-highlight-color: transparent;"
>Tabela de referência cruzada</div>

I've tried the following codes:

x = browser.find_element_by_id("Downloadcrosstab")
x = browser.find_element_by_link_text("Downloadcrosstab-Button")
x = browser.find_element_by_class_name('Crosstab')

But I got the same error:

NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".Crosstab"}
  (Session info: chrome=75.0.3770.142)
like image 748
Rafael Schettini Avatar asked Jul 18 '19 19:07

Rafael Schettini


People also ask

Where is Element ID in Selenium Python?

Python – find_element_by_id() method in Selenium In order to find element by ID find_element_by_id() method is used. Id is basically unique attribute assigned to the elements of the web page such as buttons, image, heading etc. Note :The first element with the ID attribute value matching the location will be returned.

How do I select an element by ID in Selenium?

We can find an element using the attribute id with Selenium webdriver using the locators - id, css, or xpath. To identify the element with css, the expression should be tagname[id='value'] and the method to be used is By. cssSelector. To identify the element with xpath, the expression should be //tagname[@id='value'].

How do I find an element that contains specific text in Selenium Webdriver?

We can find an element that contains specific text with Selenium webdriver in Python using the xpath. This locator has functions that help to verify a specific text contained within an element. The function text() in xpath is used to locate a webelement depending on the text visible on the page.

How can the user find the element by attribute in Selenium?

We can find an element using the attribute name with Selenium webdriver using the locators - name, css, or xpath. To identify the element with css, the expression should be tagname[name='value'] and the method to be used is By. cssSelector.


3 Answers

The element appears to be a dynamic element and to identify the element you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    x = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-tb-test-id='DownloadCrosstab-Button'][role='button']"))).click()
    
  • Using XPATH:

    x = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-tb-test-id='DownloadCrosstab-Button' and text()='Tabela de referência cruzada']")))
    
  • Note : 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
    

Note: You can find a relevant discussion in Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

like image 169
undetected Selenium Avatar answered Oct 21 '22 16:10

undetected Selenium


I actually like to add a custom html attribut for my test locators.

You can then use xpath to locate them, like //*[@data-tb-test-id="DownloadCrosstab-Button"]

like image 4
Arnaud Claudel Avatar answered Oct 21 '22 16:10

Arnaud Claudel


Use a CSS selector, specifically attribute selector like so:

https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

x = browser.find_element_by_css_selector('[data-tb-test-id="DownloadCrosstab-Button"]')

This should select an item whose data-tb-test-id is set to DownloadCrosstab-Button. If this element is not unique it will give you the first occurrence of it, so you need a more specific selector in that case

like image 1
abdusco Avatar answered Oct 21 '22 16:10

abdusco