如何在每个元素上循环

问题描述 投票:0回答:2

我正在尝试让硒单击每个化身并将其放置在每个化身的选项卡上,但我的代码无法正常工作。

driver.get("https://www.roblox.com/groups/650266/Trade#!/about")
driver.maximize_window()
driver.implicitly_wait(3)
driver.find_element_by_class_name('group-dropdown').click()
driver.find_element_by_id("role-3874587").click()
for i in range(0, 9, 10):
    driver.find_element_by_class_name("avatar-container").click()

错误消息:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

图片


  [1]: https://i.stack.imgur.com/3qAHa.png
python-3.x selenium roblox
2个回答
0
投票

我对python不太熟悉。但是,我知道Selenium和我的代码都在Java中。因此,逻辑应该相同。您需要使用此cssSelector查找WebElements

    div[class='section'] > div:nth-child(4) > ul > li

然后,将其放入列表中的这些WebElement中。然后,您需要使用for Loot进行迭代以单击每个元素。希望有帮助!

编辑:这是Java代码。

    List<WebElement> getAvatorElements = 
    driver.findElements(By.cssSelector("div[class='section'] > div:nth- 
    child(4) > ul > li"));
    for(int i=0; i< getAvatorElements.size();i++)
    {
        getAvatorElements.get(i).click();
    } 

0
投票

我认为问题出在您正在使用的for循环中。在循环中,您以0为增量从0迭代到9,应该为1或不写任何东西,只是开始和结束都可以。

for i in range(0, 9, 1):

或此

for i in range(0, 9):
© www.soinside.com 2019 - 2024. All rights reserved.