简体   繁体   English

从Python列表中单击Web元素的通用代码:

[英]Generic code for clicking web element from a list in Python:

Here is my code in Java that I want to convert in Python: 这是我要在Python中转换的Java代码:

public static void clickElementFromList(String ListObject, String strname) 
{
    List<WebElement> wbeElement;

    wbeElement = Util.findElements(ListObject);

    for (int cnt = 0; cnt < wbeElement.size(); cnt++) 
    {
        if (wbeElement.get(cnt).getText().trim().contentEquals(strname)) 
        {
            wbeElement.get(cnt).click();
            break;
        }
    }

}

//listobj is the web element. // listobj是网络元素。 //strname is the displayed text in the application // strname是应用程序中显示的文本

First of all we are not translators.. 首先我们不是翻译。

If you want to translate java code to python you have to translate it manually. 如果要将Java代码转换为python,则必须手动将其转换。 It looks like there are some tools out eg java2python but the author states 看起来好像有一些工具,例如java2python,但是作者指出

The generated Python code is not guaranteed to run, nor is guaranteed to be syntactically valid Python. 不能保证生成的Python代码可以运行,也不能保证语法上有效。

If you simply want to use a java library in a application that you want to write in python you could give jython a try. 如果您只想在要使用python编写的应用程序中使用Java库,则可以尝试jython

But as per your question and requirement to give you example/sample or a hint.. 但是根据您的问题和要求,给您示例/示例或提示。

First find all elements of list according to your condition by! 首先根据您的条件查找列表的所有元素!

from selenium import webdriver

browser = webdriver.Chrome()
browser.get(url)
# here find method will have to modified according to your condition of getting webElements
list = browser.find()

Then as per your requirement of method try something like this: 然后根据您对方法的要求尝试如下操作:

def clickElementFromList(list_object, strname):
    for name in list_object:
        text = name.text
        if text is not None:
            if text == strname:
                name.click()
                break

Then call function: 然后调用函数:

clickElementFromList(your list here, strname here)

Hope this will help you! 希望这个能对您有所帮助! :) :)

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

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