當前位置:
首頁 > 知識 > Selenium python 實現點擊非select/option下的懸浮隱藏文本

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

Selenium python 實現點擊非select/option下的懸浮隱藏文本

打開今日頭條,查看更多精彩圖片
喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

搭建SpringMVC(非web.xml文件方式)
這或許是最通俗易懂的數據一致性問題解讀

TAG:程序員小新人學習 |