Click on to remove
from selenium import webdriver #web驱动 from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver import ActionChains import time driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe') try: driver.implicitly_wait(10) driver.get('https://www.jd.com/') time.sleep(5) #点击,清除 input=driver.find_element_by_id('key') input.send_keys('围城')
# Find Search button by class
driver.find_element_by_class_name = Search ( ' Button ' ) search.click () # click the search button the time.sleep (. 3 ) INPUT2 = driver.find_element_by_id ( ' Key ' ) input2.clear () # clear input box the time.sleep ( . 1 ) input2.send_keys ( ' Murphy's Law ' ) input2.send_keys (Keys.ENTER) the time.sleep ( . 5 ) the finally : driver.close ()
About ActionChans
# Method (teleport)
from selenium import webdriver #web驱动 from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver import ActionChains import time driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe') try: driver.implicitly_wait(10) driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') time.sleep(5) driver.switch_to.frame('iframeResult') time.sleep(1)
# Target acquisition operation chain
action=ActionChains(driver)
Inspiration Box # id: draggable
source=driver.find_element_by_id('draggable')
# Target square id: droppable
target=driver.find_element_by_id('droppable')
# Inspiration box teleport to the target box
# drawn up an action, you need to call execution method
action.drag_and_drop(source,target).perform() time.sleep(10) finally: driver.close()
# Method II (shuffling)
from selenium import webdriver #web驱动 from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver import ActionChains import time driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe') try: driver.implicitly_wait(10) driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') time.sleep(5) driver.switch_to.frame('iframeResult') time.sleep(1) #启示方块id:drappable source=driver.find_element_by_id('draggable') #目标方块id:droppable target=driver.find_element_by_id('droppable')
# Find sliding distance
Print (source.size) # size Print (source.tag_name) # tag name Print (source.text) # Text Print (source.location) # coordinates: X-axis and Y # Print (target.location) Distance = target.location [ ' X ' ] -source.location [ ' X ' ]
# Pressed the start slider
ActionChains(driver).click_and_hold(source).perform()
# Cycle move
= S 0 the while S < Distance: # acquisition operation target strand # every two displacement displacement ActionChains (Driver) .move_by_offset (xoffset = 2, yoffset = 0) .perform () S + = 2 the time.sleep ( 0.1 ) # Pine starting to open the slider . ActionChains (Driver) .release () Perform () the time.sleep ( 10 ) the finally : driver.close ()
JS code
from selenium import webdriver #web驱动 from selenium.webdriver.common.keys import Keys # 键盘按键操作 import time driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe') try: driver.implicitly_wait(10) driver.get('http://www.baidu.com/') driver.execute_script( ''' alert("长歌依梦") ''' ) time.sleep(10) finally: driver.close()