简体   繁体   English

Selenium 按 Class 名称错误查找元素(Python)

[英]Selenium Find Elements by Class Name Bug (Python)

Context: This block of code is supposed to click on a web element if it has the same name as the query and clicks the first web element otherwise.上下文:如果该代码块与查询具有相同的名称,则应该单击 web 元素,否则单击第一个 web 元素。

Issue: If the else block is excluded, it works perfectly fine.问题:如果排除了 else 块,它工作得很好。 Otherwise, it just defaults to the condition in the else block ( and then throws a stale element exception after the browser closes even though it already clicked on the element??? )否则,它只是默认为 else 块中的条件(然后在浏览器关闭后抛出一个陈旧的元素异常,即使它已经点击了元素???

Reproducible: Absolutely, here's the file if needed https://github.com/The-Fag-Fajitas/Anime_Manager/blob/main/Back%20End/animemanager.py .可重现:当然,如果需要,这里是文件https://github.com/The-Fag-Fajitas/Anime_Manager/blob/main/Back%20End/animemanager.py

Note: Anything before this code works fine.注意:此代码之前的任何内容都可以正常工作。

Note 2: The purpose of this code is to check for any similar names (ie x season 4 as opposed to x or x season 1 )注意 2:此代码的目的是检查任何相似的名称(即 x season 4 而不是 x or x season 1 )

    # Only searches for the first 5 results
        an_list = dv.find_elements_by_class_name(desired_website.anime)[:5]
        for an in an_list:
            # Waits for the DOM to load (an implicit wait was already added).
            time.sleep(15)
            if an.text.lower() == query.lower():
                an.click()
            else:
                an_list[0].click()

What I think is happening is that you're getting a click which changes the page/dom.我认为正在发生的事情是你得到了一个改变页面/dom的点击。 This renders everything in an_list a stale element.这会使 an_list 中的所有内容都成为陈旧的元素。

So you either need to click into a new window (plenty of SO links to show how to do that) or break out of the for loop when you click what you want to click.因此,您要么需要单击新的 window(大量的 SO 链接来展示如何执行此操作),要么在单击要单击的内容时跳出 for 循环。

# Only searches for the first 5 results
an_list = dv.find_elements_by_class_name(desired_website.anime)[:5]
count=0;
while (count<len(an_list)):
    # Waits for the DOM to load (an implicit wait was already added).
    an = an_list[count]
    count = count+1;
    an_list = dv.find_elements_by_class_name(desired_website.anime)[:5]
    time.sleep(15)
    if an.text.lower() == query.lower():
        an.click()
    else:
        an_list[0].click()

Re find the an_list, as clicking on element changes the DOM reference重新找到 an_list,因为单击元素会更改 DOM 引用

After bashing my head against the wall for two weeks, it finally worked.在把我的头撞在墙上两周后,它终于奏效了。 Problem is, I don't know how it works.问题是,我不知道它是如何工作的。 For some reason, all stale element references suddenly disappeared and it works just fine.出于某种原因,所有过时的元素引用突然消失了,它工作得很好。 In fact, I found two solutions for it.事实上,我找到了两种解决方案。

Solution 1:解决方案1:

an_list = dv.find_elements_by_class_name(desired_website.anime)
        for an in an_list:
            if an.text.lower() == query.lower():
                an.click()
        an_list[0].click()

Solution 2:解决方案2:

an_list = dv.find_elements_by_class_name(desired_website.anime)
        for an in an_list:
            if an.text.lower() == query.lower():
                an.click()
                break
        else:
            an_list[0].click()

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

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