简体   繁体   English

使用 Python selenium 选择一个元素(文本)

[英]Select an element (text) using Python selenium

In one of my internal web applications, we have to select text in a webpage and click on the selection.在我的一个内部 Web 应用程序中,我们必须选择网页中的文本并单击选择。 then a popup opens up with more options.然后会弹出一个带有更多选项的弹出窗口。 This is a repetitive task for me and I want to automate it but selecting a web element as we do manually is not working fine for me.这对我来说是一项重复性任务,我想将其自动化,但是像我们手动选择的 Web 元素对我来说效果不佳。

Since I cannot share the web app, I have taken a webpage accessible to all and tried to recreate the problem of selection.由于我无法共享网络应用程序,因此我制作了一个所有人都可以访问的网页,并试图重现选择问题。

I am trying to simulate manual selection using mouse click at the top left of the element, then pressing Shift , then again click at end of the element.我正在尝试使用鼠标单击元素左上角,然后按Shift ,然后再次单击元素末尾来模拟手动选择。 It's not working.它不起作用。 What is wrong with the code?代码有什么问题?

Expected selection is attached as a picture:预期选择附为图片:

预期选择附为图片

Next is my code:接下来是我的代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM