Home > Software design >  Selenium Python .click() not working on one element
Selenium Python .click() not working on one element

Time:11-17

So, I am trying to make a simple project that simply just auto clicks all the details to fill out a form to get ready to post. It works perfectly up until just one element. There are 2 very similar elements; ones a category and the other is a sub-category. It states its unable to find the element, I'm fairly new to selenium so if anyone has an opinions they can give me that would be very helpful. I tried a few other methods too, using different identifiers, like its class.

from msilib.schema import Condition
from tokenize import Name
from turtle import color
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome("D:/chromedriver.exe")
driver.get("https://www.grailed.com/sell")
time.sleep(2)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/p[2]/a').click()
time.sleep(2)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/button[4]').click()
google_email = driver.find_element('xpath', '//*[@id="email"]')
google_email.send_keys("[email protected]")
driver.find_element('xpath', '//*[@id="password"]').send_keys('Pineappleguy305')
time.sleep(1)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/form/button').click()
time.sleep(3)
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/input').click()
time.sleep(2)
#Start of entering clothing details
#picking type of clothing, men or woman
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[1]').click()
time.sleep(2)
#selects the main category
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[2]').click()
time.sleep(2)
something = 'something'
driver.find_element('xpath', '//*[@id="designer-autocomplete"]').send_keys(something)
time.sleep(1)


#picking the sub-category, but this not working for some reason
#Error states unable to locate element
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[2]/div/input').click()
time.sleep(2)
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[2]/div/div/div/span[4]').click()
time.sleep(2)


CodePudding user response:

I've modified your code as below, try this:

driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/p[2]/a').click()
time.sleep(2)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/button[4]').click()
google_email = driver.find_element('xpath', '//*[@id="email"]')
google_email.send_keys("[email protected]")
driver.find_element('xpath', '//*[@id="password"]').send_keys('Pineappleguy305')
time.sleep(1)
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/form/button').click()
time.sleep(3)

title = driver.find_element(By.CSS_SELECTOR, ".SellFormHeader-Title")
driver.execute_script("arguments[0].scrollIntoView(true)",title)

driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/input').click()
time.sleep(2)
#Start of entering clothing details
#picking type of clothing, men or woman
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[1]').click()
time.sleep(2)
#selects the main category
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[2]').click()
time.sleep(2)

# Sub-category field
sub_category_option = "Tank Tops & Sleeveless"
sub_category_lists = driver.find_elements(By.XPATH, ".//*[@class='CustomDropDown-module__dropDownItems___7n7Mi']/span")
i = 0
for x in sub_category_lists:
    if x.text == sub_category_option:
        driver.find_element(By.XPATH, ".//*[@class='CustomDropDown-module__dropDownItems___7n7Mi']/span["   str(i   1)   "]").click()
        break
    i  = 1

# Designer field
option_to_select = 'Advisry Clothing'  # change the option you want to select
designer_txt_field = driver.find_element('xpath', '//*[@id="designer-autocomplete"]')
designer_txt_field.click()
designer_txt_field.send_keys("Ad")  # you have to enter some character to search and select the value of the string 'option_to_select'
time.sleep(1)

option_list = driver.find_elements(By.CSS_SELECTOR, ".Designer-module__autocomplete___fCmzc li")
time.sleep(1)

i = 0
for x in option_list:
    if x.text == option_to_select:
        driver.find_element(By.XPATH, ".//*[@class='Designer-module__autocomplete___fCmzc']/li["   str(i   1)   "]").click()
        break
    i  = 1
  • Related