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

使用Python selenium选择元素(文本),python,selenium,textselection,Python,Selenium,Textselection,在我的一个内部web应用程序中,我们必须选择网页中的文本并单击选择。然后弹出一个包含更多选项的弹出窗口。这对我来说是一项重复性的任务,我想将其自动化,但像我们手动选择web元素那样对我来说效果并不好 由于我无法共享该web应用程序,因此我选择了一个所有人都可以访问的网页,并尝试重新创建选择问题 我试图模拟手动选择,使用鼠标单击元素左上角,然后按Shift键,然后再次单击元素末尾。 它不起作用了。代码有什么问题 预期选择将作为图片附上: import selenium import time fr

在我的一个内部web应用程序中,我们必须选择网页中的文本并单击选择。然后弹出一个包含更多选项的弹出窗口。这对我来说是一项重复性的任务,我想将其自动化,但像我们手动选择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()

接下来是我的代码:

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

脚本是否应该选择从第一次出现的“动物”到段落结尾?它应该选择使用xpath(代码中的命名元素)定位的段落。我想扩展此代码以仅在页面中找到特定文本时选择元素。我为您找到了一个解决方法,您可以使用操作。拖放();我在想一个可能有用的答案。还有一个问题。我看到,在您预期的选择中,右侧的图片也被选中;但是您提供的xpath既不包含该图片,也不包含上面或下面的文本。这是一个错误,还是选择也应该包括这个?对不起,图片不应该被选择,只需要选择xpath定义的元素。