我有以下腳本:
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 financee”一詞(在它的任何位置),然后單擊(選擇)它?
提前感謝您的澄清。
uj5u.com熱心網友回復:
是的,可以將滑鼠向上移動以越過“Codice 財務”元素,但這會涉及更多的代碼行,并且可能會導致不必要的復雜性。
相反,使用諸如visibility_of_element_located()和element_to_be_clickable() 之類的expected_conditions可以使您的作業更加輕松和簡單。
操作 API
Actions API是用于向 Web 瀏覽器提供虛擬化設備輸入的低級介面。與進行額外驗證的高級元素互動不同,Actions API 提供對輸入設備的精細控制。Selenium 提供對 3 個輸入源的訪問,如下所示:
- 鍵盤操作:表示用于與網頁互動的任何鍵輸入設備。
- 滑鼠動作:表示任何與網頁互動的指標設備。
- 滾輪動作:用于與網頁互動的滾輪輸入設備的表示。
滑鼠操作 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()Move by offset:此方法將滑鼠從其當前位置(或 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()dragAndDrop:此方法首先在源元素上執行單擊并按住,移動到目標元素的位置,然后釋放滑鼠。
dragAndDropBy:此方法首先在源元素上執行單擊并按住,移動到給定的偏移量,然后釋放滑鼠。
release: This action releases the depressed left mouse button. If WebElement is passed, it will release depressed left mouse button on the given WebElement
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/449459.html
上一篇:我需要使用python從html中獲取文本,但是在html中有2個具有相同類名的元素,我需要同時獲取這兩個元素并放入一個陣列
下一篇:如何顯示一定數量的資訊。蟒蛇3
