Python Forum
Multiple input box in a webpage have same XPATH - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Multiple input box in a webpage have same XPATH (/thread-11622.html)



Multiple input box in a webpage have same XPATH - sumandas89 - Jul-18-2018

I am crawling in a webpage which have two input box in two different tabs inside the page. But the problem is that class name, id name is same for both of the input box. While I am trying to access the input box in the second tab like- clicking in the box or send some keystrokes(by send_keys()) using selenium. I am not able to access that using this below script -
driver.find_elements_by_xpath('(//*[@id="d_value"])').click()
because an exception arises that -
Error:
driver.find_elements_by_xpath('(//*[@id="d_value"])'[1]).click() AttributeError: 'list' object has no attribute 'click'
yes I know that If there are some situations like this arise xpath only can locate the first element, and it happens due to the bug of that page. But I want to know is there any solution to handle this situation using selenium and continue to crawl in the page by neglecting the bug. I searched for the solution on google but nothing happens to be beneficial for me, if anyone knows, please share with me.


RE: Multiple input box in a webpage have same XPATH - buran - Jul-18-2018

it looks like driver.find_elements_by_xpath('(//*[@id="d_value"])') returns list. Check how many elements it has. In any case, even if it is one element list, you need to get the desired element.


RE: Multiple input box in a webpage have same XPATH - sumandas89 - Jul-19-2018

(Jul-18-2018, 01:29 PM)buran Wrote: it looks like driver.find_elements_by_xpath('(//*[@id="d_value"])') returns list. Check how many elements it has. In any case, even if it is one element list, you need to get the desired element.

It seems there are two elements in the list, I printed the list using the below code-
ser = driver.find_elements_by_xpath('(//*[@id="d_value"])')
print(ser)
and got the output like below -
Output:
[<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="4c136be8-69d7-4a62-9c94-8659e3ab2190", element="425ebdba-c5c5-4a58-a31f-dfc44c753c6d")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="4c136be8-69d7-4a62-9c94-8659e3ab2190", element="fc40d0ff-34c6-4c81-8114-bd66e0e2e98d")>]
There are two input boxes in the webpage which I already mentioned, and here two list elements are printed. So it exactly matches with the number of input boxes.

But when I am doing this to select the input box-
ser = driver.find_elements_by_xpath('(//*[@id="d_value"])[1]').click()
I am getting error like below -
Error:
ser = driver.find_elements_by_xpath('(//*[@id="d_value"])[1]').click() AttributeError: 'list' object has no attribute 'click'
It means click is not applicable here in a list type object. Then how to solve the issue or how to click on a list type object and send keystrokes using send_keys().


RE: Multiple input box in a webpage have same XPATH - buran - Jul-19-2018

it should be
ser = driver.find_elements_by_xpath('(//*[@id="d_value"])')[1].click()
to get the second element
or
ser = driver.find_elements_by_xpath('(//*[@id="d_value"])')[-1].click()
to get the last one