简体   繁体   中英

How to click this element using Python and Selenium?

I have the following HTML, which I have to click

<button aria-label="Nur Ergebnisse für Inhalte anzeigen" id="ember912" class="search-vertical-filter__filter-item-button artdeco-button artdeco-button--muted artdeco-button--2 artdeco-button--tertiary ember-view" type="button">
<!---->
<span class="artdeco-button__text">
    Inhalte
</span>
</button>

id is dynamic.

I tried like this

WebDriverWait(driver, delay).until(
                EC.presence_of_element_located((By.XPATH, "//button[@aria-label='Nur Ergebnisse für Inhalte anzeigen']/button[@class='search-vertical-filter__filter-item-button artdeco-button artdeco-button--muted artdeco-button--2 artdeco-button--tertiary ember-view' and text()='Nur Ergebnisse für Inhalte anzeigen']"))).click()

and like this

WebDriverWait(driver, delay).until(
                EC.presence_of_element_located((By.XPATH, '//span[contains(text(), "Inhalte")'))).click()

and like this

WebDriverWait(driver, delay).until(
                EC.presence_of_element_located((By.XPATH, '//span[contains(text(), "Inhalte") and @class="artdeco-button__text"]'))).click()

but it does not work, I always get TimeoutException

Appreciate any help

Use the following xpath to click on the element.

WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Nur Ergebnisse fÞr Inhalte anzeigen' and contains(.,'Inhalte')]"))).click()

OR

WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label , 'Nur Ergebnisse') and contains(.,'Inhalte')]"))).click()

Note: If you get the timeout exceptions from above xpath as well then please check if there any iframe present on the webpage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM