我有以下脚本:
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/'
codes = [...] # my codes
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
time.sleep(0.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()
time.sleep(0.5)
for code in codes:
# Select Codice fiscale (= fiscal code)
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='search-type-fiscal-code']"))).click()
time.sleep(0.5)
# Clean the search bar
driver.find_element(by=By.ID, value='companySearchFormInput').clear()
time.sleep(0.5)
# Insert the fiscal code in the search bar
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='search']"))).send_keys(code)
time.sleep(0.5)
# Click on the button
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='companySearch']//input[@type='submit']"))).click()
time.sleep(0.5)
# Rest of the code
driver.close()
Selenium Python 有没有办法使用鼠标执行上述一些操作?
例如,在以下情况# Select Codice fiscale (= fiscal code)
,向上移动鼠标以移至“Codice fiscale”一词(在其任意点),然后单击(选择)它?
预先感谢您的澄清。
是的,可以将鼠标向上移动到“Codice fiscale”元素上,但这会涉及更多代码行,并且可能会导致不必要的复杂性。
相反使用预期条件 https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions like 元素可见性() https://stackoverflow.com/a/50474905/7429447 and element_to_be_clickable() https://stackoverflow.com/a/54194511/7429447可以使您的工作变得更加轻松和简单。
操作API
操作API https://www.selenium.dev/documentation/webdriver/actions_api/是向 Web 浏览器提供虚拟设备输入的低级接口。与进行额外验证的高级元素交互不同,Actions API 提供对输入设备的精细控制。 Selenium 提供对 3 个输入源的访问,如下所示:
-
键盘操作 https://www.selenium.dev/documentation/webdriver/actions_api/keyboard/:用于与网页交互的任何键输入设备的表示。
-
鼠标操作 https://www.selenium.dev/documentation/webdriver/actions_api/mouse/:用于与网页交互的任何指针设备的表示。
-
滚轮动作 https://www.selenium.dev/documentation/webdriver/actions_api/wheel/:用于与网页交互的滚轮输入设备的表示。
鼠标操作 API
几个例子鼠标操作如下面所述:
-
单击并按住:它将移动到元素并在给定元素的中间单击(不释放)。
-
上下文点击:此方法首先将鼠标移动到元素的位置,然后在给定元素上执行上下文单击(右键单击)。
-
双击:它将移动到该元素并在给定元素的中间执行双击。
-
移动到元素:该方法将鼠标移动到元素的中间。执行此操作时,该元素也会滚动到视图中。
from selenium import webdriver
driver = webdriver.Chrome()
# Navigate to url
driver.get("http://www.google.com")
# Store 'google search' button web element
gmailLink = driver.find_element(By.LINK_TEXT, "Gmail")
# Performs mouse move action onto the element
webdriver.ActionChains(driver).move_to_element(gmailLink).perform()
-
按偏移量移动:此方法将鼠标从当前位置(或 0,0)移动给定的偏移量。如果坐标位于视图窗口之外,则鼠标最终将位于浏览器窗口之外。
from selenium import webdriver
driver = webdriver.Chrome()
# Navigate to url
driver.get("http://www.google.com")
# Store 'google search' button web element
gmailLink = driver.find_element(By.LINK_TEXT, "Gmail")
# Set x and y offset positions of element
xOffset = 100
yOffset = 100
# Performs mouse move action onto the element
webdriver.ActionChains(driver).move_by_offset(xOffset,yOffset).perform()
-
拖放:该方法首先在源元素上单击并按住,移动到目标元素的位置,然后释放鼠标。
-
拖放:此方法首先在源元素上单击并按住,移动到给定的偏移量,然后释放鼠标。
-
release:此操作释放按下的鼠标左键。如果传递了 WebElement,它将释放给定 WebElement 上按下的鼠标左键
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)