输入代码,单击标签,然后使用 Python Selenium 单击按钮

发布于 2025-01-15 20:41:23 字数 1202 浏览 0 评论 0原文

我有兴趣在搜索栏中输入代码,单击标签,最后单击按钮。具体来说,该页面是 https://www.icribis.com/it/。我必须在搜索栏中输入代码(其中出现消息“Inserisci i dati dell'azienda”),然后我必须单击标签“Codice Financiale”(在搜索栏下方),最后单击带有放大镜。我的尝试:

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()

如何点击标签?另外两行是正确的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2025-01-22 20:41:23

此 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")

Update1 with ID, < strong>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更新2:

如果您只想拥有基于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()

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()
棒棒糖 2025-01-22 20:41:23

而不是这个 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文