gpt4 book ai didi

python - Lettuce Webdriver 无法通过 xpath 找到我的链接

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:25 25 4
gpt4 key购买 nike

Lettuce 选择行为驱动的“句子”并执行用于测试的代码。

有一个“网络驱动程序”可以将其与 selenium 很好地集成。

我的链接是这样的:

<div class="pull-right">
<a href="/logout">Logout ?</a>
</div>

我正在尝试使用 github readme 中描述的(任一)步骤:

I should see a link to "Google" with the url "http://google.com/"
I should see a link that contains the text "Foobar" and the url "http://foobar.com/"

与此代码相对应:

@step('I should see a link to "(.*?)" with the url "(.*?)"$')
def should_see_link_text(step, link_text, link_url):
assert_true(step,
world.browser.find_element_by_xpath(str(
'//a[@href="%s"][./text()="%s"]' %
(link_url, link_text))))


@step('I should see a link that contains the text "(.*?)" '
'and the url "(.*?)"$')
def should_include_link_text(step, link_text, link_url):
return world.browser.find_element_by_xpath(str(
'//a[@href="%s"][contains(., %s)]' %
(link_url, link_text)))

但是当我在 Chrome 开发工具中选择链接时,它会显示为以下 x 路径:

//*[@id="bs-example-navbar-collapse-1"]/div/div[2]/a

这与: '//a[@href="%s"][contains(., %s)]''//a[@href= "%s"][./text()="%s"]'.

那么我应该使用哪一步来捕获我的链接,我是否需要更改我的 html 以匹配 lettuce-webdriver 中可用的内容(提示:这不太好!)?最好不必指定 id 或类等。

编辑:误差 LWD 给出:

    Traceback (most recent call last):
File "C:\Python34\lib\site-packages\lettuce\core.py", line 144, in __call__ret = self.function(self.step, *args, **kw)
File "C:\Python34\lib\site-packages\lettuce_webdriver\webdriver.py", line 107, in should_include_link_text(link_url, link_text)))
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 230, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element {'using': by, 'value': value})['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//a[@href=\"/logout\"][contains(., \"Logout ?\")]"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensi
ons/fxdriver@googlecode.com/components/driver-component.js:9641:26)
at FirefoxDriver.prototype.findElement (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensions/fxdri
ver@googlecode.com/components/driver-component.js:9650:3)
at DelayedCommand.prototype.executeInternal_/h (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensio
ns/fxdriver@googlecode.com/components/command-processor.js:11635:16)
at DelayedCommand.prototype.executeInternal_ (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensions
/fxdriver@googlecode.com/components/command-processor.js:11640:7)
at DelayedCommand.prototype.execute/< (file:///D:/Users/CAStone/AppData/Local/Temp/tmpkxv5zfva/extensions/fxdriv
er@googlecode.com/components/command-processor.js:11582:5)

让我担心的是我的"selector":"//a[@href=\"/logout\"][contains(.,\"Logout ?\")]" 剪断。如果它正在寻找 \"/logout\"\"Logout ?\" 的确切字符串,那么它显然不会工作。

最佳答案

我会直接依赖the link text :

world.browser.find_element_by_link_text('Logout ?')

或者:

world.browser.find_element_by_partial_link_text('Logout')

关于python - Lettuce Webdriver 无法通过 xpath 找到我的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27980586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com