4

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.

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:

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

/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:

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. 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?

Any help greatly appreciated.

for completeness my unittest code is is as follows:

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()
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user3883665
  • 41
  • 1
  • 1
  • 2
  • Offtopic: don't use elements with the same name (yes its allowed, but can give a lot of quirks) http://stackoverflow.com/questions/11111670/is-it-ok-to-have-multiple-html-forms-with-the-same-name – RvdK Jul 28 '14 at 09:55

4 Answers4

8

Try this

//input[@name='action' and @value=' Search ']
wpercy
  • 9,636
  • 4
  • 33
  • 45
Ravikumar
  • 891
  • 12
  • 22
  • 1
    While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context. – Robert Columbia May 15 '18 at 19:16
4

Answering your questions,

1 - Yes you can,

driver.find_element_by_name(‘foo’)

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.

Lara
  • 2,170
  • 6
  • 22
  • 43
  • thanks, unfortunately find_element_by_name doesn't work as there are 2 elements with the same name. webdriver just selects the first one. so really I'd like to know if I can find element by name AND value as this is the unique part of the element I'm trying to get. – user3883665 Jul 28 '14 at 11:32
  • 3
    So, you should use the find_elements_by_name (you can find all elements) – Lara Jul 28 '14 at 11:36
3

Following is the Java code, which i tried with your 2 controls specified 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.

Uday
  • 1,433
  • 10
  • 36
  • 57
0
driver.find_element_by_xpath("//*[@value=' Search ']").click()

will definitely help.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
  • While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context. – Robert Columbia May 15 '18 at 19:16