Click here to Skip to main content
15,112,252 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I'm new to web scraping and I'm using Selenium to open Chrome and click a buy button on a website, but I'm not able to do this. What am I doing wrong?

Button code:

<a href="javascript:void(0)" class="cont-bt-fast buy">
                  Compra
                  28,60
               </a>


What I have tried:

<pre lang="Python">
Python
<pre>from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

url = "specific page that needs permission to access"

driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')
driver.get(url)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cont-bt-fast buy']"))).click()
Posted
Updated 1-Oct-21 10:04am
Comments
Gomesz785 30-Sep-21 2:45am
   
this probably helps you... https://stackoverflow.com/a/63242311
Gomesz785 30-Sep-21 2:48am
   
Is your button on a iframe?
Member 15371871 30-Sep-21 9:23am
   
Yes I think so, the button is inside a window on the web page. But I really don't know nothing about HTML.
Member 15371871 1-Oct-21 15:05pm
   
Thank you Gomesz785!
Gomesz785 7-Oct-21 9:37am
   
did it worked?
Member 15371871 7-Oct-21 10:47am
   
Yes.
Gomesz785 30-Sep-21 2:49am
   
https://stackoverflow.com/questions/41627971/click-javascript-button-using-selenium-python <- follow these

1 solution

The button was inside an iframe, so I need to find the iframe before the button

Python
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

url = "specific page that needs permission to access"

driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')
driver.get(url)

iframe = driver.find_element_by_xpath("/html/body/div/div/div/iframe")

driver.switch_to.frame(iframe)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cont-bt-fast buy']"))).click()
   
v2
Comments
Gomesz785 7-Oct-21 9:39am
   
did this(click the word "this") worked? Also: https://stackoverflow.com/a/41639700/17040857

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900