Reputation: 25
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.)
Upvotes: 0
Views: 149
Reputation: 5137
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"
Upvotes: 1