0

I am new to Selenium and trying to access and click 'Image' link on Google page, once it has searched some string. But I am unable to do this. Following is in Selenium Python:

driver = webdriver.Firefox()
driver.get ('http://google.com')
q = driver.find_element_by_xpath ("id('gbqfq')")     # Search text box
q.send_keys ('Apple')
driver.find_element_by_name ('btnG').click()

# I am now going to access the 'Image' link through following comamnd
driver.find_element_by_xpath ("id('hdtb_msb')/x:div[2]/x:a").click()

Error is below:

selenium.common.exceptions.InvalidSelectorException: Message: u'The given selector id(\'hdtb_msb\')/x:div[2]/x:a is either invalid or does not result in a WebElement. The following error occurred:\nInvalidSelectorError: Unable to locate an element with the xpath expression id(\'hdtb_msb\')/x:div[2]/x:a because of the following error:\n[Exception... "An attempt was made to create or change an object in a way which is incorrect with regard to namespaces" 

My questions: 1. Why is this error coming (I am using xpath Checker to get the xpath)? 2. What are other possible ways to access this link?

Thanks already!

After my research:

It looks like I have to catch the handle of the current page loaded ? Please help me, I am totally new.

5
  • I will take a detail lesson there. But here I am using "XPath Checker" and which is able to find the particular element (I am able to test it). I don't understand why is this happening then ? Commented Jan 17, 2014 at 22:03
  • @user1177636 - It would be more helpful if you told him what was wrong. Commented Jan 17, 2014 at 22:10
  • 1
    Hah, looks like I already left a snarky comment on one of his other questions. Fair enough. Commented Jan 17, 2014 at 22:12
  • I do not know what x: means in XPath. Commented Jan 17, 2014 at 22:25
  • use CSS! It's faster and cleaner! Commented Jan 17, 2014 at 22:43

2 Answers 2

2

Try removing the namespaces (ie. The x:)

Sign up to request clarification or add additional context in comments.

1 Comment

I believe this is the solution, because I also encountered the same issue today, and it is resolved by simply removing the namespace "x:". However, I don't know why the XPath given by XPath Checker has this "x" thing.
0

I believe that you are trying to search for xpath but maybe you are not passing a valid value; generally the syntax of the xpath is this one: .//*[@id='hdtb-msb']/div[2]/a

I went to google page and I check the image for example and this is its xpath: .//*[@id='hplogo']

In any case I advice you to download firebug and firepath for firefox and via this tools you retrieve the xpath that you need

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.