Good Ideas
Good Ideas

Reputation: 9

How to click button with selenium python

this is the code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
path = 'C:/Users/chromedriver-win64/chromedriver.exe'

cService = Service(path)
driver = webdriver.Chrome(service = cService)

url = 'https://kavirtire.ir/esale/login'
driver.get(url)
search_box = driver.find_element(By.CLASS_NAME,'form-control')
search_box.click()
search_box.send_keys('2948608910')
#search_box.send_keys(Keys.ENTER)
frame = driver.find_elements(By.TAG_NAME, 'iframe')
driver.switch_to.frame(frame[0])
driver.find_element(By.CLASS_NAME, 'recaptcha-checkbox-border').click()
time.sleep(2)

button = driver.find_element(By.XPATH,"/html/body/div[1]/div/div/div/div[1]/div[2]/div/form/div[3]/button").click()
time.sleep(5)

error :

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":" /html/body/div[1]/div/div/div/div[1]/div[2]/div/form/div[3]/button" (Session info: chrome=123.0.6312.86)

Can anyone solve this problem?

Upvotes: 0

Views: 43

Answers (1)

msamedozmen
msamedozmen

Reputation: 1129

The main problem is you switched your driver with this line. And your login button is not in that frame.

driver.switch_to.frame(frame[0])

So you need to switch back to your default content after you handled recaptcha. This should be work.

frame = driver.find_elements(By.TAG_NAME, 'iframe')
driver.switch_to.frame(frame[0])
driver.find_element(By.CLASS_NAME, 'recaptcha-checkbox-border').click()
driver.switch_to.default_content() #Help to switch main content.

For additional information. You can use

driver.find_element(By.CSS_SELECTOR,"btn .btn-success .float-start").

Also instead of time.sleep use WebDriverWait. Another suggestion is u don't need to click your search_box u can use send_keys() function without click.

Upvotes: 1

Related Questions