Petr Petrov
Petr Petrov

Reputation: 4452

Selenium: click to button

It's a part of html-code of page, where I should click the button

<div class="add-company-form__form-control add-company-form__submit">
    <button class="button button_theme_islands button_size_xl button_view_action button_type_submit button__control i-bem" data-bem='{"button":{}}' role="button" type="submit">
        <span class="button__text">Добавить организацию</span>
    </button>
</div>

I try to click on the button with

driver.find_element_by_xpath("//div[@class='add-company-form__submit']/button").click()

but it returns

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='add-company-form__submit']/button"}

How can I fix that?

Upvotes: 0

Views: 65

Answers (2)

Mate Mrše
Mate Mrše

Reputation: 8444

You should try instantiating a WebDriver wait before clicking the button:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.XPATH, "//button[@class='button button_theme_islands button_size_xl button_view_action button_type_submit button__control i-bem']"));

NOTE:

I used Java syntax, but it should be very similar in Python.

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Try following Xpath.

  WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='button button_theme_islands button_size_xl button_view_action button_type_submit button__control i-bem']")))

If you want to use CssSelector try this.

   WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button_theme_islands")))

Upvotes: 1

Related Questions