【问题标题】:Find elements using css selector/xpath of Selenium Webdriver with Ruby使用带有 Ruby 的 Selenium Webdriver 的 css 选择器/xpath 查找元素
【发布时间】:2013-07-29 18:05:57
【问题描述】:

我最近开始使用 Selenium 来测试一个具有 webviews 的应用程序。那么我的 webview 的代码是:

<div class="class1 class2 class3 class4" style="padding: 4px;" id="_6bd7b91a-214c-4075-b7db-dcaa08155e1a"> 
    <span class="class5" style="font-weight: bold; text-decoration: underline;">This is TITLE</span> 
    <div class="class6" dd:contenttype="content_type1" dd:concept="TITLE" id="_1d8c4490-d0c1-42ed-a93e-e92ca570dd32">
     <div class="class7"> 

我正在编写一个可重用的代码来查找用于自动化测试的元素。在上面的代码中,可以通过两种方式找到元素,我可以根据'dd:concept="TITLE"'或通过文本"This is TITLE"查找;但我在 Selenium 中找不到任何东西,我可以使用它以我想要的方式找到元素。

我正在使用“find_element”来寻找一个元素,但是我没有可以传递'dd:concept="TITLE"'"This is TITLE" 作为参数。

我试过了 - driver.find_element(:css, "span.class5")driver.find_element(:css, "div.class1 class2 class3 class4 span.class5")

我是否可以使用 Selenium 驱动程序和一些文本作为参数在 webview 上找到该元素??

【问题讨论】:

    标签: testing css-selectors selenium-webdriver web-testing ruby-test


    【解决方案1】:

    FindElement(By.CssSelector("[dd:concept='TITLE']")) 应该可以工作。

    啊,你正在使用 ruby​​。

    我看了一些,你可以试试这个:

    find_element(:css, "[dd:concept='TITLE']")

    再次编辑。

    我建议在标签名称 dd:concept 中转义 :,如下所示:

    find_element(:css, "[dd\:concept='TITLE']")

    【讨论】:

    • 谢谢理查德!但这对我没有多大帮助。我正在做 $driver.find_element(By.CssSelector("[dd:concept='TITLE']")) 并且它给了我“NameError: uninitialized constant By from (pry):3:in `block (3 levels) in '" 错误。
    • 我猜 find_element 采用的唯一参数是其中之一 - (:class, :class_name, :css, :id, :link_text, :link, :partial_link_text, :name, :tag_name, : xpath)
    • 我试过这样。我收到“Selenium::WebDriver::Error::UnknownError:处理命令时发生未知的服务器端错误。”错误
    • 通过一些谷歌搜索,我发现我可以使用它 - driver.find_element(:css, "div.ddsection ddrefreshable ddinsertfreetext ddremovable span.ddsectiondisplay")。但即使这对我也不起作用。
    • 这对我有用。昨天它没有工作,因为某种原因我没有在 dom 中获得所需的文本。
    猜你喜欢
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2015-09-13
    相关资源
    最近更新 更多