-1

Been struggling on this for a little while now and I'm pulling my hairout. Why does the 1st code not work? But second one does?

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


driver = webdriver.Chrome(executable_path='/Users/jackrossanderson/Desktop/chromedriver')
driver.get('https://www.url.Iwanttoscrape.com')
search = driver.find_element_by_name('searchbar')
search.send_keys("hometown")
search.send_keys(Keys.RETURN)




try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.Class_Name, 'button i want to click'))
    )
    element.click()

    driver.back()
except:
    print(error)'''

'''from selenium.webdriver.common.keys import Keys
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.by import By
import time


driver = webdriver.Chrome(executable_path='/Users/jackrossanderson/Desktop/chromedriver')
driver.get('https://www.urlIwanttoscrape')
search = driver.find_element_by_name('searchbar')
search.send_keys("home town")
search.send_keys(Keys.RETURN)

time.sleep(12)
element = driver.find_element_by_class_name('button I want to press')

element.click()
driver.back()'''

I know the second way is frowned upon but I cant see why the top one doesnt work?

Jando94
  • 3
  • 2

1 Answers1

0

You can try using element = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CLASS_NAME, "button i want to click"))) to make the driver wait and click based on the class name

Use, element_to_be_clickable is recommended as mentioned here because, presence_of_element_located description defines that it does not necessarily mean the element is visible or clickable.

Also, check the casing for By.CLASS_NAME used.

Bala
  • 24
  • 3