0

Right now I have a problem using Selenium in Python. This is the first time I am using it, so if I am doing something horribly wrong, please tell me. Anyways, I would like the script below to click a button on a website. Not that hard it seems, but for some reason it always gives me this error:

selenium.common.exceptions.InvalidSelectorException was unhandled by user code
Message: Message: The given selector id('panel1-7')/x:div[1]/x:button is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression id('panel1-7')/x:div[1]/x:button because of the following error:
NamespaceError: An attempt was made to create or change an object in a way which is incorrect with regard to namespaces
Stacktrace:
    at FirefoxDriver.annotateInvalidSelectorError_ (file:///C:/Users/USER/AppData/Local/Temp/tmpd47h5z77/extensions/[email protected]/components/driver-component.js:10744)
    at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/USER/AppData/Local/Temp/tmpd47h5z77/extensions/[email protected]/components/driver-component.js:10775)
    at FirefoxDriver.prototype.findElement (file:///C:/Users/USER/AppData/Local/Temp/tmpd47h5z77/extensions/[email protected]/components/driver-component.js:10779)
    at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/USER/AppData/Local/Temp/tmpd47h5z77/extensions/[email protected]/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/USER/AppData/Local/Temp/tmpd47h5z77/extensions/[email protected]/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///C:/Users/USER/AppData/Local/Temp/tmpd47h5z77/extensions/[email protected]/components/command-processor.js:12608)

Here is the code I am using

##Import Modules
##
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import unittest
import time

##Initialize and Define Variables
##
driver = webdriver.Firefox()
##Open up webpage
driver.get("http://PretendThisIsTheTargetWebsitePlease.com")
RedButtonXpath = "id('panel1-7')/x:div[1]/x:button"
BlackButtonXpath = "id('panel8-14')/x:div[1]/x:button"
GreenButtonXpath = "id('panel0-0')/x:div[1]/x:button"
RedButtonFieldElement = WebDriverWait(driver, 2).until(lambda driver: driver.find_element_by_xpath(RedButtonXpath))
BlackButtonFieldElement = WebDriverWait(driver, 2).until(lambda driver: driver.find_element_by_xpath(BlackButtonXpath))
GreenButtonFieldElement = WebDriverWait(driver, 2).until(lambda driver: driver.find_element_by_xpath(GreenButtonXpath))

##Main Loop
##
input = input("Generic answer here please! :D >>> ")

I changed the website that I am using for privacy reasons, but as far as I can see, the actual XPath isn't the problem.

(Also, please tell me if you see any way to speed up or make the script more efficient in any way.)

4
  • One thing you can do is dumping the html from selenium webdriver before you look for the buttons, which let you check if they are really there when you call the finders. Commented Mar 22, 2016 at 0:34
  • @Mai How do you do that? Commented Mar 22, 2016 at 0:53
  • Are you sure that HTML of your application has a custom namespace "x"? Commented Mar 22, 2016 at 0:56
  • @Buaban No, it doesn't, thank you very much. Your answer fixed everything. Commented Mar 22, 2016 at 1:07

1 Answer 1

1

Your XPath expressions are incorrect. They contain namespace "x" (I have no idea why you put this "x" in expression). So the solution is just remove "x" from your XPath expressions. See below:

RedButtonXpath = "id('panel1-7')/div[1]/button"
BlackButtonXpath = "id('panel8-14')/div[1]/button"
GreenButtonXpath = "id('panel0-0')/div[1]/button"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.