Selenium Python 2 find an item by name and value?

I am trying to find an element on a web page, this element has the same name as the other elements, and there is no identifier. It has a different meaning, so I would like to find an element named AND Value. I'm new to this, so naked with me ...

on my webpage 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 cannot use the name because I have another element with the same name and type:

<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">

So I tried using xpath, I started by checking xpath to return the returned 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 quite new to this, but I assume that "x:" should not be in the way, so I deleted it and tried to find an element with the following:

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()

as a result:

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, I have 2 questions, it would be great if someone helped:

  • ?
  • , - xpath, xpath? , ?

.

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
3

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

Below is the Java code that I tried with the two controls above.

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.

+3
source

All Articles