Ich versuche, eine Webseite mit Selen und Python zu automatisieren. Ich habe 'xpath checker - https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/' in Firefox verwendet und den folgenden Code geschrieben.Wie behebt man das Problem "InvalidSelectorError: Fehler beim Suchen eines Elements mit dem XPath-Ausdruck"?
Selen-Python-Code:
def create_user():
#Click 'Administration'
mydriver.find_element_by_xpath("//*[@id('main-navigation')/x:li[5]/x:a").click()
mydriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
mydriver.find_element_by_xpath("//*[@id('main-navigation')/x:li[5]/x:ul/x:li[1]/x:ul[2]/x:li[2]/x:a").click()
mydriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
mydriver.find_element_by_xpath("//*[@id('accordion')/x:li[2]/x:a").click()
mydriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
create_user()
Wenn ich die oben ausführen, erhalte ich folgende Fehlermeldung:
InvalidSelectorException: Message: The given selector //*[@id('main-navigation')
/x:li[5]/x:a 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('main-navigation')/x:li[5]/x:a because of the following error:
SyntaxError: The expression is not a legal expression.
Stacktrace:
at FirefoxDriver.annotateInvalidSelectorError_ (file:///c:/users/admin/appda
ta/local/temp/2/tmpgchoem/extensions/[email protected]/components/driver-c
omponent.js:10744)
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/admin/appd
ata/local/temp/2/tmpgchoem/extensions/[email protected]/components/driver-
component.js:10775)
at FirefoxDriver.prototype.findElement (file:///c:/users/admin/appdata/local
/temp/2/tmpgchoem/extensions/[email protected]/components/driver-component
.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/admin/appda
ta/local/temp/2/tmpgchoem/extensions/[email protected]/components/command-
processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/admin/appdata
/local/temp/2/tmpgchoem/extensions/[email protected]/components/command-pr
ocessor.js:12666)
at DelayedCommand.prototype.execute/< (file:///c:/users/admin/appdata/local/
temp/2/tmpgchoem/extensions/[email protected]/components/command-processor
.js:12608)
Könnte jemand bitte eine Idee vorschlagen, diesen Code zu beheben.
Warum Sie verwenden 'x:' .... und es ist '@ id = 'main-navigation'' insteadof' @id (' main-Navigation ') '... –