Selenium python 实现点击非select/option下的悬浮隐藏文本
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 random
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
import sys
import re
import datetime
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
dr=webdriver.Chrome()
def is_visible(locator, timeout=10):
try:
WebDriverWait(dr, timeout).until(EC.visibility_of_element_located((By.XPATH, locator)))
return True
except TimeoutException:
return False
def highlight(driver,element):
driver.execute_script("arguments[0].setAttribute('style',arguments[1]);",
element,"border:3px solid red;")
dr.get('https://www.amazon.com/gp/site-directory?ref=nav_shopall_btn')
dr.maximize_window()
is_visible('//select[@id="searchDropdownBox"]')
element=dr.find_element_by_xpath('//select[@id="searchDropdownBox"]')
# highlight(dr,element)
element.click()
time.sleep(2)
try:
# Select(dr.find_element_by_xpath('//select[@id="searchDropdownBox"]')).select_by_visible_text("Books")
# 通过索引定位
Select(dr.find_element_by_xpath('//select[@id="searchDropdownBox"]')).select_by_index(50)
time.sleep(1)
dr.find_element_by_xpath('//div[@class="nav-search-submit nav-sprite"]/input').click()
time.sleep(3)
element2 = dr.find_element_by_xpath('//div[@id="nav-shop"]/a/span[2]')
time.sleep(10)
highlight(dr,element2)
ActionChains(dr).move_to_element(element2).perform()
time.sleep(3)
ActionChains(dr).move_by_offset(244, 82).perform()
time.sleep(3)
ActionChains(dr).move_by_offset(0,50).click()
time.sleep(3)
except Exception as e:
print(str(e))