Home > Net >  Enter a code, click on a label and then on a button with Python Selenium
Enter a code, click on a label and then on a button with Python Selenium

Time:03-22

I am interested in entering a code in a search bar, clicking on a label and finally clicking on a button. Specifically, the page is https://www.icribis.com/it/. I have to enter the code in the search bar (where the message "Inserisci i dati dell'azienda" appears), then I have to click on the label "Codice fiscale" (under the search bar) and finally click on the button with the magnifying glass. My attempt:

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
import time

url = 'https://www.icribis.com/it/'

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)

time.sleep(2)

# Disable cookies
driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")').click()

# Enter the code
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'clearfix')]//input[@name='search']"))).send_keys("my_code")

# Click on the label
# ...

# Click on the button with the magnifying glass
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'media-obj-right')]//input[@type='submit']"))).click()

How can I click on a label? And the other two lines are correct?

CodePudding user response:

This xpath:

//div[contains(@class,'clearfix')]//input[@name='search']

does not have any match, you should use this id:

companySearchFormInput

or XPath:

//input[@id='companySearchFormInput']

Code:

# Enter the code
wait.until(EC.visibility_of_element_located((By.ID, "companySearchFormInput"))).send_keys("my_code")

or

# Enter the code
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@id='companySearchFormInput']"))).send_keys("my_code")

Update1 with ID, CSS_SELECTOR:

time.sleep(5)

# Disable cookies
driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")').click()

# Enter the code
wait.until(EC.visibility_of_element_located((By.ID, "companySearchFormInput"))).send_keys("my_code")

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='search-type-fiscal-code']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Cerca']"))).click()

Update2 with Xpath:

If you want to have only XPath based solution:

time.sleep(5)

# Disable cookies
driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")').click()

# Enter the code
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='companySearchFormInput']"))).send_keys("my_code")

wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='search-type-fiscal-code']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Cerca']"))).click()

CodePudding user response:

instead of this XPath locator

//div[contains(@class,'clearfix')]//input[@name='search']

You can use this

//input[@name='search']

Also this XPath is not unique

//div[contains(@class,'media-obj-right')]//input[@type='submit']

Try using

//div[@id='companySearch']//input[@type='submit']

Instead So your code could be:

# Enter the code
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='search']"))).send_keys("my_code")
# Click on the button with the magnifying glass
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='companySearch']//input[@type='submit']"))).click()
  • Related