[英]Select an element (text) using Python selenium
在我的一個內部 Web 應用程序中,我們必須選擇網頁中的文本並單擊選擇。 然后會彈出一個帶有更多選項的彈出窗口。 這對我來說是一項重復性任務,我想將其自動化,但是像我們手動選擇的 Web 元素對我來說效果不佳。
由於我無法共享網絡應用程序,因此我制作了一個所有人都可以訪問的網頁,並試圖重現選擇問題。
我正在嘗試使用鼠標單擊元素左上角,然后按Shift ,然后再次單擊元素末尾來模擬手動選擇。 它不起作用。 代碼有什么問題?
預期選擇附為圖片:
接下來是我的代碼:
import selenium
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
print("program has started")
# Customary code starts
myDriver=selenium.webdriver.Chrome("D:\\Software\\ChromeDriver\\chromedriver.exe")
myDriver.set_page_load_timeout(60)
myDriver.maximize_window()
myDriver.implicitly_wait(30)
# Customary code ends
# opening website
myDriver.get("https://en.wikipedia.org/wiki/Animal")
time.sleep(4)
actions = ActionChains(myDriver)
element=myDriver.find_element_by_xpath("//*[@id='mw-content-text']/div/p[2]")
actions.move_to_element(element)
myDriver.switch_to_active_element()
time.sleep(5)
actions.move_by_offset(-(element.size["width"])/2, -(element.size["height"])/2)
actions.click()
ActionChains(myDriver).key_down(Keys.SHIFT)
actions.move_by_offset((element.size["width"]),(element.size["height"]))
actions.click()
# actions.context_click()
actions.perform()
# print(element.size["width"])
# print(element.size["height"])
# print(element.location["x"])
# print(element.location["y"])
# print(element.location)
print("end of action")
print("Sucessfully came to the end")
#myDriver.quit()
# calling a action method as A
a=A(self.driver)
# Click on text box and enter some text
self.driver.find_element_by_xpath("/html/body/div[3]/div[3]/div/div/div/div/div[1]/main/div/form/div[1]/div[2]/div/div/div/div[1]/section/div[1]/div/div/div/div[2]/div/div/div/div/div/div").send_keys("amar")
time.sleep(2)
# performing control action
a.key_down(K.CONTROL).send_keys("a").perform()
time.sleep(2)
# selecting the text
a.key_down(K.CONTROL).send_keys("c").perform()
time.sleep(5)
#click on bold and appl
self.driver.find_element_by_xpath("/html/body/div[3]/div[3]/div/div/div/div/div[1]/main/div/form/div[1]/div[2]/div/div/div/div[1]/section/div[1]/div/div/div/div[1]/div[1]/div[1]").click()
time.sleep(5)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.