S
S
san_m_m2021-10-06 21:09:52
Python
san_m_m, 2021-10-06 21:09:52

How to click on a button using Selenium?

Good afternoon
I can not figure out how to click on the button, as it occurs in several parts of the page.
I tried that, but it doesn't work...

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time

driver = webdriver.Chrome(executable_path='D:\chromedriver.exe')

driver.get("https://www.soglasie.ru/agent_search/")
search = driver.find_element_by_xpath("//input[@class='input-smart__input']")
search.send_keys('Иван')
button = driver.find_element_by_xpath("//div[@class='s-btn']")
button.click()

#button_c = driver.find_element_by_tag_name("div.s-btn__content")[7].click()
#Такой вариант не работает 

try:
    main = WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.TAG_NAME, "div.check"))
    )
    print(main.text)
except:
    driver.quit()

driver.quit()


And also the question of how to make it so that you click on the disclosure of the table as much as necessary to disclose all the data ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-10-07
@san_m_m

At least specify which button to press
. Through XPath, you can find it as follows:

driver.find_element_by_xpath("//span[text()='Показать еще']")

Further, you can already do it in a simple way, so as not to bother with all sorts of Wait
from selenium.common.exceptions import NoSuchElementException

search = driver.find_element_by_xpath("//input[@class='input-smart__input']")
search.send_keys('Иван')
button = driver.find_element_by_xpath("//div[@class='s-btn']")
button.click()

while True:

    while True:
        # Если данные все еще загружаются, классы кнопки меняются на `s-btn__loader`
        if driver.find_elements_by_class_name('s-btn__loader'):
            time.sleep(5)
        else:
            break

    try:
        load_more_btn = driver.find_element_by_xpath("//span[text()='Показать еще']")
    except NoSuchElementException:
        print('Загрузили страницу до конца')
        break
    
    load_more_btn.click()

    time.sleep(5)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question