Selenium Python 2查找元素的名称和值?

时间:2022-11-15 19:31:19

I have been trying to locate an element on a web page, this element has the same name as other elements and no id. It does have a different value, so I would like to find the element by the name AND the Value. I'm pretty new to this so bare with me...

我一直试图在web页面上定位一个元素,这个元素与其他元素的名称相同,并且没有id。它的值不同,所以我希望找到元素的名称和值。我对这个赤裸裸的话题很陌生……

in my web page I have a search button:

在我的网页上,我有一个搜索按钮:

<input name="action" value=" Search " style=" border-style2;  font-size: 11;
                     font-family: arial, helvetica" border="0" type="submit">

I can't use name because I have another element with the same name and type:

我不能使用name,因为我有另一个名称和类型相同的元素:

<input name="action" value=" Go " onclick=" return custRange('cust', 'd_custno');"
                     style=" border-style2;  font-size: 11; font-family: arial, helvetica"
                     border="0" type="submit">

I therefore tried to use xpath, I started by using the xpath checker to return the xpath which returned:

因此,我尝试使用xpath,我开始使用xpath检查器返回返回的xpath:

/x:html/x:body/x:center/x:table/x:tbody/x:tr/x:td/x:center/x:center[1]/x:table[2]/x:tbody/x:tr/x:td[1]/x:table[3]/x:tbody/x:tr[5]/x:td[2]/x:input[2]

Again pretty new to this but I'm assuming the "x:" shouldn't be in the path so I removed this and tried to find the element with the following:

这又是一个全新的概念,但我假设x不应该在路径中,所以我去掉了这个,并试图找到下面的元素:

search = driver.find_element_by_xpath("//center[1]/table[2]/tbody/tr/td[1]/table[3]/tbody/tr[5]/td[2]/input[2]")
search.clickAndWait()

which resulted in:

导致:

selenium.common.exceptions.NoSuchElementException: Message: 'Unable to find element with xpath == //center[1]/table[2]/tbody/tr/td[1]/table[3]/tbody/tr[5]/td[2]/input[2]'

So really I have 2 questions it would be great if someone could help with:

所以我有两个问题如果有人能帮忙就好了

  1. is there a way to identify an element by name and value?
  2. 有按名称和值标识元素的方法吗?
  3. obviously I'm missing something with xpath, is the way I'm trying to use xpath incorrect? if so could you suggest what I'm doing wrong?
  4. 很明显,我在xpath上漏掉了一些东西,我使用xpath的方式是错误的吗?如果是这样,你能告诉我我做错了什么吗?

Any help greatly appreciated.

任何帮助深表感谢。

for completeness my unittest code is is as follows:

我的unittest代码的完整性如下:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class umLoginTest(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Ie()
        self.driver.implicitly_wait(3)

    def test_login_to_um(self):
        driver = self.driver
        result_file = open("umLoginTest_result.txt", "w")
        driver.get("http://ps-iweb/UserManage/Home")
        self.assertIn("Welcome", driver.title)
        elem = driver.find_element_by_name("userLogin")
        elem.send_keys("pstevenson")
        password = driver.find_element_by_name("sec_password")
        password.send_keys("etc")
        password.send_keys(Keys.RETURN)
        test1 = driver.title
        result_file.write(str(test1))
        select = driver.find_element_by_name("conn_marketid")
        for option in select.find_elements_by_tag_name('option'):
            if option.text == 'Int Rate Swaps':
                option.click()
        search = driver.find_element_by_xpath("//center[1]/table[2]/tbody/tr/td[1]/table[3]/tbody/tr[5]/td[2]/input[2]")
        search.clickAndWait()

        result_file.close()

if __name__ == "__main__":
    unittest.main()

4 个解决方案

#1


8  

Try this

试试这个

//input[@name='action' and @value=' Search ']

#2


3  

Answering your questions,

回答你的问题,

1 - Yes you can,

1 -你可以,

driver.find_element_by_name(‘foo’)

http://selenium-python.readthedocs.org/en/latest/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_name

http://selenium-python.readthedocs.org/en/latest/api.html selenium.webdriver.remote.webdriver.WebDriver.find_element_by_name

2 - I always try to avoid xpath because sometimes it doesn't work properly. Instead, you should always try to find by name or id .. I think is the best case.

2 -我总是尽量避免使用xpath,因为有时它不能正常工作。相反,你应该尽量按名字或id查找。我认为这是最好的情况。

#3


3  

Following is the Java code, which i tried with your 2 controls specified above.

下面是Java代码,我尝试了上面指定的两个控件。

driver= new FirefoxDriver();
driver.get("file:///D:/Programming%20Samples/Input.html");
driver.findElement(By.xpath("//*[@name='action'][@value='Go']")).click();
driver.findElement(By.xpath("//*[@name='action'][@value='Search']")).click();  //observed here in your HTML i see some spaces. Make sure giving the right value.

Hope above helps.

希望上面的帮助。

#4


0  

driver.find_element_by_xpath("//*[@value=' Search ']").click()

will definitely help.

肯定会有帮助。

#1


8  

Try this

试试这个

//input[@name='action' and @value=' Search ']

#2


3  

Answering your questions,

回答你的问题,

1 - Yes you can,

1 -你可以,

driver.find_element_by_name(‘foo’)

http://selenium-python.readthedocs.org/en/latest/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_element_by_name

http://selenium-python.readthedocs.org/en/latest/api.html selenium.webdriver.remote.webdriver.WebDriver.find_element_by_name

2 - I always try to avoid xpath because sometimes it doesn't work properly. Instead, you should always try to find by name or id .. I think is the best case.

2 -我总是尽量避免使用xpath,因为有时它不能正常工作。相反,你应该尽量按名字或id查找。我认为这是最好的情况。

#3


3  

Following is the Java code, which i tried with your 2 controls specified above.

下面是Java代码,我尝试了上面指定的两个控件。

driver= new FirefoxDriver();
driver.get("file:///D:/Programming%20Samples/Input.html");
driver.findElement(By.xpath("//*[@name='action'][@value='Go']")).click();
driver.findElement(By.xpath("//*[@name='action'][@value='Search']")).click();  //observed here in your HTML i see some spaces. Make sure giving the right value.

Hope above helps.

希望上面的帮助。

#4


0  

driver.find_element_by_xpath("//*[@value=' Search ']").click()

will definitely help.

肯定会有帮助。