我有兴趣在搜索栏中输入代码,单击标签,最后单击按钮。具体来说,该页面是https://www.icribis.com/it/.我必须在搜索栏中输入代码(出现消息"Inserisci I dati dell’azienda"(,然后我必须单击标签";Codice fiscale";(在搜索栏下(,最后用放大镜点击按钮。我的尝试:
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()
如何单击标签?另外两行是正确的吗?
此xpath:
//div[contains(@class,'clearfix')]//input[@name='search']
没有任何匹配,您应该使用此id:
companySearchFormInput
或XPath:
//input[@id='companySearchFormInput']
代码:
# Enter the code
wait.until(EC.visibility_of_element_located((By.ID, "companySearchFormInput"))).send_keys("my_code")
或
# Enter the code
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@id='companySearchFormInput']"))).send_keys("my_code")
带有ID的Update1,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()
使用Xpath的Update2:
如果您只想拥有基于XPath的解决方案:
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()
而不是此XPath定位器
//div[contains(@class,'clearfix')]//input[@name='search']
你可以使用这个
//input[@name='search']
此外,此XPath不是唯一的
//div[contains(@class,'media-obj-right')]//input[@type='submit']
尝试使用
//div[@id='companySearch']//input[@type='submit']
相反所以你的代码可能是:
# 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()