My goal is to log into a page (https://www.icribis.com/it/) by entering my email address and password. The first step is to disable cookies, the second is to click on the button "ACCEDI" at the top right. At this point I have to enter my email and password and click on the button "Accedi" button. I miss this last part. This is my partial code:
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()
time.sleep(2)
# Click the button "ACCEDI"
driver.execute_script('return document.querySelector("body > section.menu > div.row.show-for-xlarge-up > div.large-10.medium-6.small-12.columns > div.large-12.medium-12.small-12.columns.text-right.menuTop > ul > li.openClick.text-left > a")').click()
time.sleep(2)
# Enter your email and password and click the button "Accedi"
# ...
driver.close()
I would like to solve through the use of XPath, as their writing seems simpler and more compact to me, but how they work is still not very clear to me.
How can I finish my script?
CodePudding user response:
You do not need to use JavaScript query in order to open the login dialog
driver.execute_script('return document.querySelector("body > section.menu > div.row.show-for-xlarge-up > div.large-10.medium-6.small-12.columns > div.large-12.medium-12.small-12.columns.text-right.menuTop > ul > li.openClick.text-left > a")').click()
It can be done with regular Selenium .click()
method with use of Expected Conditions explicit wait.
In this case you will not need the hardcoded pauses time.sleep(2)
you are using here.
This code works and looks better:
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()
#login
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li.openClick.text-left"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@style,'block')]//input[@name='email']"))).send_keys("your_email")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@style,'block')]//input[@name='password']"))).send_keys("your_password")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@style,'block')]//input[@type='submit']"))).click()
CodePudding user response:
You can use the below after to enter email, password and click on Accedi button.
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='dx']//input[@placeholder='Indirizzomail']"))).send_keys("EMAIL")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='dx']//input[@placeholder='Password']"))).send_keys("PASSWORD")
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@class='button tiny lightRound rightIcon login']"))).click()