Click on a button on web page using Selenium - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Click on a button on web page using Selenium (/thread-37742.html) |
Click on a button on web page using Selenium - Pavel_47 - Jul-16-2022 Hello, Here is example where I'm looking for a solution to click on button "Lire plus" on this page: 1 km à pied Here is my snippet: from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By options = Options() options.add_argument("--headless") browser = webdriver.Chrome('/usr/bin/chromedriver', options=options) url = 'https://www.arte.tv/fr/videos/103475-006-A/1-km-a-pied/' browser.get(url) buttons = browser.find_elements(By.TAG_NAME, "button") for button in buttons: if button.text == "Lire plus": button.click()Here is output: Any suggestions.Thanks. RE: Click on a button on web page using Selenium - snippsat - Jul-16-2022 (Jul-16-2022, 02:34 PM)Pavel_47 Wrote: Here is example where I'm looking for a solution to click on button "Lire plus" on this page:You most by most more specific than just button there are many buttons on site.Don't use --headless when test stuff out or will not see if it work.Test this work. from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.action_chains import ActionChains import time #--| Setup options = Options() #options.add_argument("--headless") ser = Service(r"C:\cmder\bin\chromedriver.exe") browser = webdriver.Chrome(service=ser, options=options) #--| Parse or automation url = "https://www.arte.tv/fr/videos/103475-006-A/1-km-a-pied/" browser.get(url) time.sleep(5) lire = browser.find_element(By.CSS_SELECTOR, '#__next > div > main > div.css-p6yk9l > div.css-5mui6u > div > button') lire.click() time.sleep(5) lire_close = browser.find_element(By.CSS_SELECTOR, '#__next > div > main > div.css-p6yk9l > div.css-5mui6u > div > button') lire_close.click() RE: Click on a button on web page using Selenium - Pavel_47 - Jul-17-2022 Thanks. Tried your approach. Doesn't work - the similar message: In fact what I'm looking for is to access data that only becomes visible if the "Lire plus" section is expanded.There is probably another solution. Anyway, when I search for data related to "Lire plus" (ex. Réalisation, Scénario), it is not found. RE: Click on a button on web page using Selenium - Pavel_47 - Jul-17-2022 Tried to use WebDriverWait. Doesn't work either. from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException, ElementClickInterceptedException import time options = Options() driver = webdriver.Chrome('/usr/bin/chromedriver', options=options) url = 'https://www.arte.tv/fr/videos/103475-006-A/1-km-a-pied/' driver.get(url) timeout = 10 try: button_lire_plus = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#__next > div > main > div.css-p6yk9l > div.css-5mui6u > div > button'))) button_lire_plus.click() except TimeoutException: print('Failed to locate button') except ElementClickInterceptedException: print('Can\'t click on button') finally: driver.quit() driver.quit()Here is output in shell:
RE: Click on a button on web page using Selenium - snippsat - Jul-17-2022 Try serval time something i get ElementClickInterceptedException. Now just use sleep(5) as test selenium as owns waits Parse test of content in Lire plus. from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.action_chains import ActionChains import time #--| Setup options = Options() #options.add_argument("--headless") ser = Service(r"C:\cmder\bin\chromedriver.exe") browser = webdriver.Chrome(service=ser, options=options) #--| Parse or automation url = "https://www.arte.tv/fr/videos/103475-006-A/1-km-a-pied/" browser.get(url) time.sleep(5) lire = browser.find_element(By.CSS_SELECTOR, '#__next > div > main > div.css-p6yk9l > div.css-5mui6u > div > button') lire.click() time.sleep(3) content = browser.find_element(By.CSS_SELECTOR, 'div.css-5pryf8 > div.css-m3r1o3 > div.css-vhqfin') print(content.text)
RE: Click on a button on web page using Selenium - Pavel_47 - Jul-17-2022 Tried. Still get this: Perhaps should I change some options in Chrome ?
RE: Click on a button on web page using Selenium - Pavel_47 - Jul-17-2022 Well ... here is solution that works also at my side: from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException, ElementClickInterceptedException options = Options() driver = webdriver.Chrome() url = 'https://www.arte.tv/fr/videos/103475-006-A/1-km-a-pied/' driver.get(url) timeout = 10 button_location = '#__next > div > main > div.css-p6yk9l > div.css-5mui6u > div > button' try: button_lire_plus = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, button_location))) driver.execute_script('arguments[0].click()', button_lire_plus) content = driver.find_element(By.CSS_SELECTOR, 'div.css-5pryf8 > div.css-m3r1o3 > div.css-vhqfin') print(content.text) except TimeoutException: print('Failed to locate button') except ElementClickInterceptedException: print('Can\'t click on button') finally: driver.quit() driver.quit()I'm wondering does exist some other solution allowing to access this data in headless mode ? RE: Click on a button on web page using Selenium - ellapurnellrt - Jan-05-2023 Ok seems I have solved the problem. |