【问题标题】:How to loop on each element如何循环每个元素
【发布时间】:2020-10-03 13:44:56
【问题描述】:

我正在尝试让 selenium 单击每个头像并将其放在每个头像的选项卡上,但我的代码不起作用。

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

【问题讨论】:

  • 只是一个问题,为什么你使用``` getAvatorElements.get(i).click(); ``` get(i) 是做什么的?

标签: python-3.x selenium roblox


【解决方案1】:

我对python不是很熟悉。但是,我知道 Selenium 和我的代码是用 Java 编写的。所以,逻辑应该是一样的。你需要通过这个 cssSelector 找到 WebElements

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

然后,将这些WebElements放入List中。然后,您需要通过使用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();
    } 

【讨论】:

    【解决方案2】:

    我认为问题出在您使用的 for 循环中。 在循环中,你从 0 迭代到 9,增量为 10,它应该是 1,或者不写任何东西,只是 start 和 end 会起作用。

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

    或者这个

    for i in range(0, 9):
    

    【讨论】:

    • 它有效,但我希望它遍历他们列出的组中当前括号中的所有人,例如我想找到富有的交易者我希望它遍历所有人民。这就是我想要做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2017-10-07
    • 1970-01-01
    • 2021-08-24
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多