Home > Software design >  Python selenium is neither not looking for id nor selecting button or giving input
Python selenium is neither not looking for id nor selecting button or giving input

Time:08-13

So when I am execute my code the website will be open but the other steps which I described in my code below are not be execute, why? I even tried time.sleep() after the website is loaded in order to execute the remaining code but it is not working.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


driver = webdriver.Chrome('/Users/User/Desktop/it_projects/python-google-automation/Neuer Ordner/chromedriver')

#open certain website
driver.get('https://www.nike.com/de/?cp=58194921917_search_|nike|10594878138|107792850434|e|c|DE|pure|452291007809&ds_rl=1252249&gclid=EAIaIQobChMIod-_o8jD-QIVE4XVCh1-FggsEAAYASAAEgJSYfD_BwE&gclsrc=aw.ds')



#go through cookie process
evade_cookie = driver.find_element_by_id('hf_cookie_text_moreInformation') #search for cookie-button (more informations)
evade_cookie.send_keys(Keys.RETURN) #click on button
select_cookie = driver.find_element_by_id('hf_cookie_label_done') #search for done-button
select_cookie.send_keys(Keys.RETURN) #click on done-button 

#search for Sneakers
search = driver.find_element_by_id('VisualSearchInput') #search for input-area
search.send_keys('Nike Dunk Low') #insert input
search.send_keys(Keys.RETURN) #enter or return respectively
Error
/Users/user/Desktop/it_projects/python-google-automation/Neuer Ordner/main.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome('/Users/user/Desktop/it_projects/python-google-automation/Neuer Ordner/chromedriver')
Traceback (most recent call last):
  File "/Users/user/Desktop/it_projects/python-google-automation/Neuer Ordner/main.py", line 14, in <module>
    evade_cookie = driver.find_element_by_id('hf_cookie_text_moreInformation') #search for cookie-button (more informations)
AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'
user@MacBook-Air-von-Sami python-google-automation % 

CodePudding user response:

It seems that the syntax you are using is incorrect.

Try the below code

#go through cookie process
evade_cookie = driver.find_element('id','hf_cookie_text_moreInformation') #search for cookie-button (more informations)
evade_cookie.send_keys(Keys.RETURN) #click on button
select_cookie = driver.find_element('id','hf_cookie_label_done') #search for done-button
select_cookie.send_keys(Keys.RETURN) #click on done-button 

CodePudding user response:

The following code will properly wait for the elements to be clickable, and execute correctly. The setup is Firefox/geckodriver on Linux, but you can adapt it to your own, just observe the imports, and the part after defining the browser/driver:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t

firefox_options = Firefox_Options()

driverService = Service('chromedriver/geckodriver')

browser = webdriver.Firefox(service=driverService, options=firefox_options)

url = 'https://www.nike.com/de/?cp=58194921917_search_|nike|10594878138|107792850434|e|c|DE|pure|452291007809&ds_rl=1252249&gclid=EAIaIQobChMIod-_o8jD-QIVE4XVCh1-FggsEAAYASAAEgJSYfD_BwE&gclsrc=aw.ds'

browser.get(url) 

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="Mehr Informationen"]'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="Fertig"]'))).click()

search_box = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,'VisualSearchInput')))
search_box.send_keys('Nike Dunk Low')
t.sleep(1)
search_box.send_keys(Keys.RETURN)

Selenium documentation: https://www.selenium.dev/documentation/

  • Related