S
S
san_m_m2021-10-24 11:27:56
Python
san_m_m, 2021-10-24 11:27:56

Part 2. How to click on a button using Selenium?

Good afternoon!

The problem is that in order for the code to open all pages to work correctly... You must click on the button to accept the cookie.

Below I attach the code itself and the solutions to the problem that I tried.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains



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

driver.get("https://www.soglasie.ru/agent_search/")
time.sleep(4)

#button_cookie = driver.find_element_by_xpath("//span[text()='Согласен']")
#button_cookie = driver.find_elements_by_class_name('cookie-policy__button')
#button_cookie.click()
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:
        if driver.find_elements_by_class_name('s-btn__loader'):
            time.sleep(10)
        else:
            break

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

    except NoSuchElementException:
        print('Загрузили страницу до конца')
        break
    

    load_more_btn.click()

    time.sleep(10)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sanya Hihi Haha, 2021-10-24
@san_m_m

btn = find_element(By.XPATH, "you xpath or some other way to find the element")
driver.execute_script("arguments[0].click();", btn )

P
Python Newbie, 2021-10-24
@Levman5

She had to be found. Here is the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains



driver = webdriver.Chrome(executable_path='chromedriver.exe')

driver.get("https://www.soglasie.ru/agent_search/")
time.sleep(4)
driver.find_element_by_class_name('cookie-policy__button').click()
#button_cookie = driver.find_element_by_xpath("//span[text()='Согласен']")
#button_cookie = driver.find_elements_by_class_name('cookie-policy__button')
#button_cookie.click()
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:
        if driver.find_elements_by_class_name('s-btn__loader'):
            time.sleep(10)
        else:
            break

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

    except NoSuchElementException:
        print('Загрузили страницу до конца')
        break
    

    load_more_btn.click()

    time.sleep(10)

driver.find_element_by_class_name('cookie-policy__button').click()
looks for a button and clicks on it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question