如何在watir中调用div下的div类?

时间:2022-11-28 09:12:53

I am creating automated test scripts with Ruby and Watir.

我正在使用Ruby和Watir创建自动化测试脚本。

I am trying to call a div class from these lines of codes:

我试图从这些代码行调用div类:

<div class="slimScrollDiv" style="position: relative; overflow: hidden; width: auto; height: 500px;">

  <div class="modal-body" style="overflow: hidden; width: auto; height: 500px;">
    <form id="user-form" class="form-inline" enctype="multipart/form-data" method="POST" novalidate="novalidate">
        <fieldset></fieldset>
    </form>
  </div>

My aim is to use this one browser.div(:class => '').send_keys :space because I want to scroll the div, for me to be able to send_key "" in some other textfield hidden. I even try to use browser.scroll.to :bottom, but still it doesn't work.

我的目标是使用这个browser.div(:class =>'')。send_keys:space因为我想滚动div,因为我能够在隐藏的其他一些文本字段中发送send_key。我甚至尝试使用browser.scroll.to:bottom,但它仍然不起作用。

2 个解决方案

#1


1  

hidden text_field or just not visible in page?

隐藏的text_field或只是在页面中不可见?

you could try focus:

你可以试试焦点:

browser.text_field(:id => 'myid').focus

or if you need to scrolldown until it is visible

或者如果你需要向下滚动直到它可见

unless browser.text_field(:id => 'myid').visible?   
  browser.div(:class => 'slimScrollDiv').send_keys :arrow_down
end

or if you need to scrolldown to the footer (end of page)

或者如果你需要向下滚动到页脚(页面末尾)

 browser.div(:id => 'footer').wd.location_once_scrolled_into_view

#2


2  

So I am making some guesses as to what you are really trying to accomplish here. I'll first address your question as originally stated, then address how to do what I think you are trying to accomplish.

所以我正在猜测你在这里要做的事情。我将首先解决您最初提出的问题,然后解决如何做我认为您想要完成的事情。

Selecting elements inside other elements The high level answer is that there is nothing special to doing this, in many (most) cases you don't even need to worry about how deeply nested an element is, as long as you have a unique way to identify it, such as a class, name, etc that is unique.. For example in the code you provided, to get the div with class modal-body you would just do

选择其他元素中的元素高级答案是没有什么特别的做法,在许多(大多数)情况下,你甚至不需要担心元素嵌套的深度,只要你有一个独特的方式识别它,例如类,名称等是唯一的..例如在你提供的代码中,为了获得带有类模态体的div,你只需要做

browser.div(:class => 'modal-body)

If there were a lot of divs on the page with that class, then you might want to locate the one you want by looking inside some other container element that is unique so you find the right one.. in the code above that would be something like

如果页面上有很多div与该类,那么你可能想要找到你想要的那个,通过查看一个独特的其他容器元素,所以你找到正确的..在上面的代码中将是一些东西喜欢

browser.div(:class => 'slimScrollDiv').div(:class => 'modal-body)

Usually you only do that sort of thing when it is difficult to find an easy way to identify an element because it has few or no attributes, or non-unique attributes.

通常只有在很难找到识别元素的简单方法时才会这样做,因为它很少或没有属性或非唯一属性。

Scrolling a custom control This I think is your real issue. You appear to have a control with a non-standard scrollbox. That is to say that instead of being simple HTML that is defined so that the browser creates and controls a scroll box (which would normally work with the methods you have already tried, or even get scrolled into view auto_magically) you instead have a custom control where the scroll box is driven by javascript code such as jQuery or Bootstrap. Such code usually is event driven, looking for things like mousewheel movement when the mouse is over the control, or manually grabbing and dragging the element that is being rendered to look like a scrollbar. Often such controls do NOT respond to keypresses like space or arrows, EVEN if they have been selected, clicked on, have focus etc.

滚动自定义控件我认为这是您真正的问题。您似乎拥有一个非标准滚动条的控件。也就是说,不是简单的HTML定义,以便浏览器创建和控制滚动框(通常可以使用您已尝试过的方法,甚至可以自动滚动到视图中),而是使用自定义控件滚动框由javascript代码(如jQuery或Bootstrap)驱动。这样的代码通常是事件驱动的,当鼠标悬停在控件上时,查找鼠标滚轮移动等内容,或者手动抓取并拖动正在渲染的元素看起来像滚动条。通常这样的控件不响应空格或箭头等按键,即使它们已被选中,点击,有焦点等。

Still I would first suggest try using .location_once_scrolled_into_view on the sub element you want that is inside the scrollbox. (See example in other answer) If that does not work then try manipulating the controls of the sçroll area.

我仍然建议尝试在你想要的子元素上使用.location_once_scrolled_into_view在滚动框内。 (参见其他答案中的示例)如果不起作用,则尝试操作söroll区域的控件。

Based on class values in your code fragment, I think you may be dealing with a jQuery powered 'slimScroll' control similar to those shown on this page. If that is indeed the case, then you can scroll the control by using drag methods on the scrollbar element. In slimScroll the element you want will be inside the div with class 'slimScrollDiv' and is another div with the class 'slimScrollBar'.

基于代码片段中的类值,我认为您可能正在处理类似于此页面上显示的jQuery驱动的“slimScroll”控件。如果确实如此,则可以使用滚动条元素上的拖动方法滚动控件。在slimScroll中,你想要的元素将在div中使用类“slimScrollDiv”,并​​且是另一个具有类“slimScrollBar”的div。

As long as the scrollbar is visible, you should be able to do a .drag_and_drop_by on it to scroll the content in the scrollbox. If it is not visible (as can happen in the first example on the linked page) then it may be harder, you might have to try firing mouseover events, or invoking some JS code to change it's style to not be hidden. You will need to experiment to see how many pixels to drag it at a time to scroll the window a 'right' amount based on how much content is in there. (potentially you might be able to figure that out based on the .style 'height' values for the elements, but that would take experimentation with the actual page and only needed if the amount of content in the box is variable)

只要滚动条可见,您就应该能够在其上执行.drag_and_drop_by以滚动滚动框中的内容。如果它不可见(可能发生在链接页面上的第一个示例中)那么它可能更难,您可能必须尝试触发鼠标悬停事件,或调用一些JS代码来更改它的样式以便不被隐藏。您需要尝试查看一次拖动多少像素,以根据窗口中的内容滚动窗口“正确”的数量。 (可能你可能能够根据元素的.style'height'值来计算出来,但这需要对实际页面进行实验,只有在框中的内容量可变时才需要)

For the second example on the linked page, I was able to scroll things using the following code

对于链接页面上的第二个示例,我可以使用以下代码滚动内容

scrollbar = browser.div(:class => 'slimScrollBar', :index => 1)
scrollbar.drag_and_drop_by 0,10

I'm not sure what is inside your scrollbox, but for psuedo-code purposes of we just call it 'thing_I_want' then you might be able to scroll it into view with code something like this

我不确定你的滚动条里面有什么,但是对于psuedo-code的目的我们只是称它为'thing_I_want'然后你可以将它滚动到视图中,代码类似这样

scrollbar = browser.div(:class => 'slimScrollBar')
until thing_I_want.present?
  scrollbar.drag_and_drop_by 0,5
end

as I said you might have to play with the drag distance there to get a 'right' amount, but 5 (pixels) seemed a reasonable starting place.

正如我所说,你可能不得不在那里使用拖动距离来获得“正确”的数量,但是5(像素)似乎是一个合理的起点。

#1


1  

hidden text_field or just not visible in page?

隐藏的text_field或只是在页面中不可见?

you could try focus:

你可以试试焦点:

browser.text_field(:id => 'myid').focus

or if you need to scrolldown until it is visible

或者如果你需要向下滚动直到它可见

unless browser.text_field(:id => 'myid').visible?   
  browser.div(:class => 'slimScrollDiv').send_keys :arrow_down
end

or if you need to scrolldown to the footer (end of page)

或者如果你需要向下滚动到页脚(页面末尾)

 browser.div(:id => 'footer').wd.location_once_scrolled_into_view

#2


2  

So I am making some guesses as to what you are really trying to accomplish here. I'll first address your question as originally stated, then address how to do what I think you are trying to accomplish.

所以我正在猜测你在这里要做的事情。我将首先解决您最初提出的问题,然后解决如何做我认为您想要完成的事情。

Selecting elements inside other elements The high level answer is that there is nothing special to doing this, in many (most) cases you don't even need to worry about how deeply nested an element is, as long as you have a unique way to identify it, such as a class, name, etc that is unique.. For example in the code you provided, to get the div with class modal-body you would just do

选择其他元素中的元素高级答案是没有什么特别的做法,在许多(大多数)情况下,你甚至不需要担心元素嵌套的深度,只要你有一个独特的方式识别它,例如类,名称等是唯一的..例如在你提供的代码中,为了获得带有类模态体的div,你只需要做

browser.div(:class => 'modal-body)

If there were a lot of divs on the page with that class, then you might want to locate the one you want by looking inside some other container element that is unique so you find the right one.. in the code above that would be something like

如果页面上有很多div与该类,那么你可能想要找到你想要的那个,通过查看一个独特的其他容器元素,所以你找到正确的..在上面的代码中将是一些东西喜欢

browser.div(:class => 'slimScrollDiv').div(:class => 'modal-body)

Usually you only do that sort of thing when it is difficult to find an easy way to identify an element because it has few or no attributes, or non-unique attributes.

通常只有在很难找到识别元素的简单方法时才会这样做,因为它很少或没有属性或非唯一属性。

Scrolling a custom control This I think is your real issue. You appear to have a control with a non-standard scrollbox. That is to say that instead of being simple HTML that is defined so that the browser creates and controls a scroll box (which would normally work with the methods you have already tried, or even get scrolled into view auto_magically) you instead have a custom control where the scroll box is driven by javascript code such as jQuery or Bootstrap. Such code usually is event driven, looking for things like mousewheel movement when the mouse is over the control, or manually grabbing and dragging the element that is being rendered to look like a scrollbar. Often such controls do NOT respond to keypresses like space or arrows, EVEN if they have been selected, clicked on, have focus etc.

滚动自定义控件我认为这是您真正的问题。您似乎拥有一个非标准滚动条的控件。也就是说,不是简单的HTML定义,以便浏览器创建和控制滚动框(通常可以使用您已尝试过的方法,甚至可以自动滚动到视图中),而是使用自定义控件滚动框由javascript代码(如jQuery或Bootstrap)驱动。这样的代码通常是事件驱动的,当鼠标悬停在控件上时,查找鼠标滚轮移动等内容,或者手动抓取并拖动正在渲染的元素看起来像滚动条。通常这样的控件不响应空格或箭头等按键,即使它们已被选中,点击,有焦点等。

Still I would first suggest try using .location_once_scrolled_into_view on the sub element you want that is inside the scrollbox. (See example in other answer) If that does not work then try manipulating the controls of the sçroll area.

我仍然建议尝试在你想要的子元素上使用.location_once_scrolled_into_view在滚动框内。 (参见其他答案中的示例)如果不起作用,则尝试操作söroll区域的控件。

Based on class values in your code fragment, I think you may be dealing with a jQuery powered 'slimScroll' control similar to those shown on this page. If that is indeed the case, then you can scroll the control by using drag methods on the scrollbar element. In slimScroll the element you want will be inside the div with class 'slimScrollDiv' and is another div with the class 'slimScrollBar'.

基于代码片段中的类值,我认为您可能正在处理类似于此页面上显示的jQuery驱动的“slimScroll”控件。如果确实如此,则可以使用滚动条元素上的拖动方法滚动控件。在slimScroll中,你想要的元素将在div中使用类“slimScrollDiv”,并​​且是另一个具有类“slimScrollBar”的div。

As long as the scrollbar is visible, you should be able to do a .drag_and_drop_by on it to scroll the content in the scrollbox. If it is not visible (as can happen in the first example on the linked page) then it may be harder, you might have to try firing mouseover events, or invoking some JS code to change it's style to not be hidden. You will need to experiment to see how many pixels to drag it at a time to scroll the window a 'right' amount based on how much content is in there. (potentially you might be able to figure that out based on the .style 'height' values for the elements, but that would take experimentation with the actual page and only needed if the amount of content in the box is variable)

只要滚动条可见,您就应该能够在其上执行.drag_and_drop_by以滚动滚动框中的内容。如果它不可见(可能发生在链接页面上的第一个示例中)那么它可能更难,您可能必须尝试触发鼠标悬停事件,或调用一些JS代码来更改它的样式以便不被隐藏。您需要尝试查看一次拖动多少像素,以根据窗口中的内容滚动窗口“正确”的数量。 (可能你可能能够根据元素的.style'height'值来计算出来,但这需要对实际页面进行实验,只有在框中的内容量可变时才需要)

For the second example on the linked page, I was able to scroll things using the following code

对于链接页面上的第二个示例,我可以使用以下代码滚动内容

scrollbar = browser.div(:class => 'slimScrollBar', :index => 1)
scrollbar.drag_and_drop_by 0,10

I'm not sure what is inside your scrollbox, but for psuedo-code purposes of we just call it 'thing_I_want' then you might be able to scroll it into view with code something like this

我不确定你的滚动条里面有什么,但是对于psuedo-code的目的我们只是称它为'thing_I_want'然后你可以将它滚动到视图中,代码类似这样

scrollbar = browser.div(:class => 'slimScrollBar')
until thing_I_want.present?
  scrollbar.drag_and_drop_by 0,5
end

as I said you might have to play with the drag distance there to get a 'right' amount, but 5 (pixels) seemed a reasonable starting place.

正如我所说,你可能不得不在那里使用拖动距离来获得“正确”的数量,但是5(像素)似乎是一个合理的起点。