输入代码,单击标签,然后单击带有 Python Selenium 的按钮
Enter a code, click on a label and then on a button with Python Selenium
我有兴趣在搜索栏中输入代码,点击标签,最后点击按钮。具体来说,页面是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 更新 1,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 与 Xpath:
如果您只想使用 基于 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()
我有兴趣在搜索栏中输入代码,点击标签,最后点击按钮。具体来说,页面是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 更新 1,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 与 Xpath:
如果您只想使用 基于 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()