1

I have the following URL from which I want to pass some product number and get the current URL

https://www.skroutz.gr/

the form has

<form method="get" action="/search" accept-charset="utf-8">
  <p>
    <span class="search-bar-input-wrapper">
      <input type="search" name="keyphrase" id="search-bar-input" value="" autocomplete="off" autocorrect="off" autocapitalize="off" placeholder="γράψε τον όρο αναζήτησης">
      <span id="search-spinner" class="spinner"></span>
      <span class="icon clear-search hidden"></span>
    </span>
    <button type="submit" value="submit">
      Αναζήτηση
    </button>
  </p>
</form>

I used the following to submit search argument

records = []
listings =[]

Users = ['Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev>(KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev>',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36']
RandomAgent = np.random.choice(Users)
opts = Options()
opts.add_argument("user-agent=["+RandomAgent+"]")
br = webdriver.Chrome('C:\\chromedriver.exe',options=opts)


def current_time():
    return datetime.datetime.now().strftime('%d-%m-%Y %H:%M')

def mycontents():
    contents = []
    with open('sku_list.csv', 'r',encoding='utf-8',newline='') as csvf:
        reader = csv.reader(csvf, delimiter=";")
        for row in reader:
            contents.append(row)  
        return contents


def get_the_link(myskus):

    br.get('https://www.skroutz.gr')
    for item in myskus:  
        search_url = br.find_element_by_id('search-bar-input')
        search_url.clear()
        search_url.send_keys(item) 
        WebDriverWait(br,5).until(EC.element_to_be_clickable((By.XPATH,'//button[@value="submit"]'))).click()
        records.append(br.current_url)
    return records

but I get an error that the element cannot be found

How can I submit the form?

thank you

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Maria Georgali
  • 629
  • 1
  • 9
  • 22

2 Answers2

2

Induce WebDriverWait and element_to_be_clickable() and below xpath.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

for item in myskus:

   search_url = br.find_element_by_id('search-bar-input')
   search_url.clear()
   search_url.send_keys(item)
   WebDriverWait(br,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@value="submit"]'))).click()
   records.append(br.current_url)

This is the entire code I have tried and it provides me the output.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
br=webdriver.Chrome()
br.get("https://www.skroutz.gr/")
myskus=['test','test1','abc']
records=[]
for item in myskus:

   search_url = br.find_element_by_id('search-bar-input')
   search_url.clear()
   search_url.send_keys(item)
   WebDriverWait(br,10).until(EC.element_to_be_clickable((By.XPATH,'//button[@value="submit"]'))).click()
   records.append(br.current_url)

print(records)

Output:

['https://www.skroutz.gr/search?keyphrase=test', 'https://www.skroutz.gr/search?keyphrase=test1', 'https://www.skroutz.gr/search?keyphrase=abc']
KunduK
  • 32,888
  • 5
  • 17
  • 41
1

To click() on the element with text as Αναζήτηση you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using XPATH 1 and click():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(@action, 'search')]//button[.,'Αναζήτηση']"))).click()
    
  • Using XPATH 2 and click():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(@action, 'search')]//button[normalize-space()='Αναζήτηση']"))).click()
    
  • Using XPATH 1 and submit():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(@action, 'search')]//button[.,'Αναζήτηση']"))).submit()
    
  • Using XPATH 2 and submit():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(@action, 'search')]//button[normalize-space()='Αναζήτηση']"))).submit()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352