简体   繁体   English

如何随机 select 并使用 selenium webdriver(Python)单击元素?

[英]How to randomly select and click on element with selenium webdriver (Python)?

I have to randomly select an element from todo list app and click to delete it.我必须从待办事项列表应用程序中随机 select 一个元素,然后单击以将其删除。 Basically, every element has the same selector, just index changes (eg//*[@class="destroy"])[2]).基本上,每个元素都有相同的选择器,只是索引更改(例如//*[@class="destroy"])[2])。 I have code that generates random sentences and adds them to the app.我有生成随机句子并将它们添加到应用程序的代码。 Then I have to randomly select an element -> click on it to delete and here I got stuck.然后我必须随机 select 一个元素 -> 点击它删除,我就卡在这里了。 Random.choice selects a random number and actually, it works. Random.choice 选择一个随机数,实际上它可以工作。 But sometimes if it randomly selects [0] the test fails and test execution takes a lot of time and I believe there is a more professional way to make a random choice.但有时如果它随机选择 [0] 测试失败并且测试执行需要大量时间,我相信有更专业的方法来进行随机选择。 Could you please advise other possible ways?您能否建议其他可能的方法?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import random
import os
 
@keyword
    def open_browser(self):
        os.environ['MOZ_HEADLESS'] = '1'
        self.driver = webdriver.Firefox()
        self.driver.get('http://todomvc.com/examples/react/#/')
        self.driver.implicitly_wait(20)
@keyword
    def generate_random_sentences(self, number, total_items_amount):
        words = [["One", "Two", "Three", "Four"], ["red", "yellow", "green", "blue"],
                 ["cats", "dogs", "zebras", "pandas"], ["jumped.", "danced.", "wrote poetry.", "cried."]]
        for i in range(int(number)):
            sentences = ' '.join([random.choice(w) for w in words])
            self.driver.find_element(By.CLASS_NAME, "new-todo").send_keys(sentences, Keys.ENTER)
        total_items = self.driver.find_element(By.CLASS_NAME, "todo-count")
        assert total_items.text == total_items_amount```

@keyword
    def delete_random_element(self, total_items_amount):
        i = random.choice(range(51))
        list_item = self.driver.find_element(By.XPATH, '(//*[@class="view"]){}'.format([i])) 
        hidden_button = self.driver.find_element(By.XPATH, '(//*[@class="destroy"]){}'.format([i])) 
        actions = ActionChains(self.driver)
        actions.move_to_element(list_item)
        actions.click(hidden_button)
        actions.perform()
        total_items = self.driver.find_element(By.CLASS_NAME, "todo-count")
        assert total_items.text == total_items_amount```

You can use random.randint(start, end) to specify a range of integers that can be randomly selected and pass that as the index.您可以使用random.randint(start, end)指定可以随机选择的整数范围并将其作为索引传递。

random_element = f'//*[@class="destroy"])[{random.randint(startint,endint)}]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver单击或选择Python中输入下拉框的第一个元素(值) - how to click or select on very first element(value) of input dropdown box in python using selenium webdriver 如何使用 Selenium WebDriver 单击该元素 - How to click that element with Selenium WebDriver 如何在selenium webdriver Python中用鼠标点击第二层元素? - How to click on the 2nd layer of element with mouse in selenium webdriver Python? 如何通过Selenium Webdriver Python选择“排序依据”元素 - How can I select the “Sort By” element through Selenium Webdriver Python 元素未在Odoo中单击python selenium webdriver中的单击 - Element is not clicking on click in python selenium webdriver in Odoo Selenium Webdriver (Python) - 单击 div 元素(复选框) - Selenium Webdriver (Python) - Click on a div element (checkbox) 找到Python Selenium Webdriver元素,但无法单击它 - Python Selenium Webdriver element found but can not click on it Selenium Webdriver Python - 如何在随机更改的 JS 按钮中单击正确的数字? - Selenium Webdriver Python - how to click the right number in randomly changed JS buttons? 如何使用 selenium webdriver select 元素样式 - How to select an element style with selenium webdriver python selenium webdriver“没有这样的元素” - python selenium webdriver “no such element”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM