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.
JavaScript
x
41
41
1
from msilib.schema import Condition
2
from tokenize import Name
3
from turtle import color
4
from selenium import webdriver
5
from selenium.webdriver.common.keys import Keys
6
import time
7
driver = webdriver.Chrome("D:/chromedriver.exe")
8
driver.get("https://www.grailed.com/sell")
9
time.sleep(2)
10
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/p[2]/a').click()
11
time.sleep(2)
12
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/button[4]').click()
13
google_email = driver.find_element('xpath', '//*[@id="email"]')
14
google_email.send_keys("@gmail.com")
15
driver.find_element('xpath', '//*[@id="password"]').send_keys('')
16
time.sleep(1)
17
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/form/button').click()
18
time.sleep(3)
19
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/input').click()
20
time.sleep(2)
21
#Start of entering clothing details
22
#picking type of clothing, men or woman
23
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[1]').click()
24
time.sleep(2)
25
#selects the main category
26
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[2]').click()
27
time.sleep(2)
28
something = 'something'
29
driver.find_element('xpath', '//*[@id="designer-autocomplete"]').send_keys(something)
30
time.sleep(1)
31
32
33
#picking the sub-category, but this not working for some reason
34
#Error states unable to locate element
35
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[2]/div/input').click()
36
time.sleep(2)
37
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[2]/div/div/div/span[4]').click()
38
time.sleep(2)
39
40
41
Advertisement
Answer
I’ve modified your code as below, try this:
JavaScript
1
50
50
1
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/p[2]/a').click()
2
time.sleep(2)
3
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/button[4]').click()
4
google_email = driver.find_element('xpath', '//*[@id="email"]')
5
google_email.send_keys("argelarroyo2001@gmail.com")
6
driver.find_element('xpath', '//*[@id="password"]').send_keys('Pineappleguy305')
7
time.sleep(1)
8
driver.find_element('xpath', '//*[@id="app"]/div[7]/div/div/div/div[2]/div/div/form/button').click()
9
time.sleep(3)
10
11
title = driver.find_element(By.CSS_SELECTOR, ".SellFormHeader-Title")
12
driver.execute_script("arguments[0].scrollIntoView(true)",title)
13
14
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/input').click()
15
time.sleep(2)
16
#Start of entering clothing details
17
#picking type of clothing, men or woman
18
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[1]').click()
19
time.sleep(2)
20
#selects the main category
21
driver.find_element('xpath', '//*[@id="SellForm"]/div/div[2]/form/div[1]/div/div[1]/div[1]/div/div/span[2]').click()
22
time.sleep(2)
23
24
# Sub-category field
25
sub_category_option = "Tank Tops & Sleeveless"
26
sub_category_lists = driver.find_elements(By.XPATH, ".//*[@class='CustomDropDown-module__dropDownItems___7n7Mi']/span")
27
i = 0
28
for x in sub_category_lists:
29
if x.text == sub_category_option:
30
driver.find_element(By.XPATH, ".//*[@class='CustomDropDown-module__dropDownItems___7n7Mi']/span[" + str(i + 1) + "]").click()
31
break
32
i += 1
33
34
# Designer field
35
option_to_select = 'Advisry Clothing' # change the option you want to select
36
designer_txt_field = driver.find_element('xpath', '//*[@id="designer-autocomplete"]')
37
designer_txt_field.click()
38
designer_txt_field.send_keys("Ad") # you have to enter some character to search and select the value of the string 'option_to_select'
39
time.sleep(1)
40
41
option_list = driver.find_elements(By.CSS_SELECTOR, ".Designer-module__autocomplete___fCmzc li")
42
time.sleep(1)
43
44
i = 0
45
for x in option_list:
46
if x.text == option_to_select:
47
driver.find_element(By.XPATH, ".//*[@class='Designer-module__autocomplete___fCmzc']/li[" + str(i + 1) + "]").click()
48
break
49
i += 1
50