GVKun编程网logo

如何使用Selenium WebDriver单击列表中的特定元素?(selenium单选按钮)

9

在这里,我们将给大家分享关于如何使用SeleniumWebDriver单击列表中的特定元素?的知识,让您更了解selenium单选按钮的本质,同时也会涉及到如何更有效地css–如何使用Selenium

在这里,我们将给大家分享关于如何使用Selenium WebDriver单击列表中的特定元素?的知识,让您更了解selenium单选按钮的本质,同时也会涉及到如何更有效地css – 如何使用Selenium WebDriver,Java按文本选择Web元素、Java 如何强制Selenium WebDriver单击当前不可见的元素?、ruby – 无法使用Selenium WebDriver单击Safari中的按钮、Selenium WebDriver WebDriverWait如何等待一组相同的元素?的内容。

本文目录一览:

如何使用Selenium WebDriver单击列表中的特定元素?(selenium单选按钮)

如何使用Selenium WebDriver单击列表中的特定元素?(selenium单选按钮)

  1. 我有一个名字来自Excel工作表
  2. 具有此特定名称的元素列表,我需要单击此元素,这是我的代码:
        for(WebElement li : myElements)     {         System.out.println(li.getText());         System.out.println();         for (int i=0; i<Name.length(); i++)         {            if(li.getText().equalsIgnoreCase(Name[i]))            {               System.out.println("Matched");              //myElements.get(2).click();            }          }    }

这是我的html代码:

       <div>            <ul>                <liid="radListBoxSource_i0">                    <span>Apex</span></li><liid="radListBoxSource_i1">                    <span>ARC</span></li><liid="radListBoxSource_i2">                    <span>Buckeye</span></li><liid="radListBoxSource_i3">                    <span>Citgo</span>                </li>        <liid="radListBoxSource_i4">           <span>Colonial</span>        </li>        <liid="radListBoxSource_i5">           <span>KMEP North</span>        </li>        <liid="radListBoxSource_i6">           <span>KMEP South</span>        </li>        <liid="radListBoxSource_i7">            <span>KMEP West</span>        </li>        <liid="radListBoxSource_i8">            <span>Magellan</span>        </li>        <liid="radListBoxSource_i9">            <span>Magellan East</span>        </li>        <liid="radListBoxSource_i10">            <span>Magellan West</span>        </li>        <liid="radListBoxSource_i11">            <span>Marathon</span>        </li>        <liid="radListBoxSource_i12">            <span>Marathon East</span>        </li>        <liid="radListBoxSource_i13">            <span>Motiva</span>        </li>        <liid="radListBoxSource_i14">            <span>Motiva Shell</span>        </li>        <liid="radListBoxSource_i15">            <span>Motiva South</span>        </li>        <liid="radListBoxSource_i16">            <span>TPSI</span>        </li>        <liid="radListBoxSource_i17">            <span>TPSI East</span>        </li>        <liid="radListBoxSource_i18">            <span>Vecenergy</span>        </li>        </ul>       </div>

这个名称从excel表格中以’Motiva’的形式出现。

谁能帮我解决这个问题?

答案1

小编典典

事情就这样了:假设您的myElements将如下所示:

    List<WebElement> myElements =driver.findElements(By.cssSelector("li[class*=''rlbItem'']"));    for(WebElement li : myElements)     {         System.out.println(li.getText());         System.out.println();         for (int i=0; i<Name.length(); i++)         {            if(li.getText().equalsIgnoreCase(Name[i]))            {                //Clicks on the matched webelement                    li.click();            }          }    }

css – 如何使用Selenium WebDriver,Java按文本选择Web元素

css – 如何使用Selenium WebDriver,Java按文本选择Web元素

我需要在网页上找到以下元素
<div>noreply@somedomain.com</div>

我正在使用Java编写Selenium WebDriver.
需要这个元素的确切CSS选择器才能将它与driver.findElement(By.cssSelector(the-selector).click命令一起使用.
div [class =’b-datalist__item__addr’]选择器不够好,因为我必须根据noreply@somedomain.com搜索不是链接的文本,所以我不能使用findElement(By.linkText())命令.

解决方法

Css不允许您进行基于文本的搜索. xpath是唯一的选择.
//div[contains(text(),'noreply@somedomain.com')]

Java 如何强制Selenium WebDriver单击当前不可见的元素?

Java 如何强制Selenium WebDriver单击当前不可见的元素?

我在FirefoxDriver中使用Selenium 2 Java API。当我填写表单时,根据表单输入,将复选框添加到页面。

我想使用Selenium模拟对这些复选框的单击。该元素在常规浏览器中可见且可用,但是selenium断言该元素不可见。

"Element is not currently visible and so may not be interacted with"

我可以强迫selenium忽略元素的不可见状态吗?如何强制Selenium与不可见元素进行交互?

答案1

小编典典

Selenium根据以下条件确定元素是否可见(使用DOM检查器确定哪些CSS适用于你的元素,请确保你查看计算的样式):

  • visibility != hidden
  • display != none (is also checked against every parent element)
  • opacity != 0 (this is not checked for clicking an element)
  • height and width are both > 0
  • for an input, the attribute type != hidden

你的元素符合这些条件之一。如果你没有能力更改元素的样式,请按照以下方法用javascript强制执行此操作(由于你使用了Selenium2 API,因此假设使用WebDriver):

((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", inputElement);

但这不会触发javascript事件,如果你依赖于该输入的change事件,那么你也必须触发它(有多种方法,最容易使用该页面上加载的任何javascript库)。

ruby – 无法使用Selenium WebDriver单击Safari中的按钮

ruby – 无法使用Selenium WebDriver单击Safari中的按钮

我想点击下面的按钮但是当我没有点击运行脚本按钮时,脚本仍在传递.

这是HTML剪辑

<input type="button" value="Authorize"id="oauth2button">

这是我用来执行点击操作的ruby代码

authorize_button = $driver.find_element(:id,'oauth2button')
authorize_button.click

如果我使用authorize_button.displayed?它返回true.
我使用的是Selenium WebDriver,Safari版本是6.1.1

解决方法

这可能不是您正在寻找的答案,但是当我遇到这类问题时,我做过的一件事就是使用JavaScript来执行特定的操作而不是Selenium,这对我来说每次都有用.你可以尝试类似的东西:

$driver.execute_script("document.getElementById('oauth2button').click();")

Selenium WebDriver WebDriverWait如何等待一组相同的元素?

Selenium WebDriver WebDriverWait如何等待一组相同的元素?

如果你知道每页的元素数量,你可以使用这个函数来等待所有预期的元素:

from selenium.common.exceptions import TimeoutException,StaleElementReferenceException

def wait_until_all_expected_elements(func,number_of_elements,timeout=30):

endtime = time.time() + timeout
while True:
    try:
        if time.time() > endtime:
            raise TimeoutException("The function doesn't return a sufficient number of elements")
        elements = func()
        if len(elements) == number_of_elements:
            return elements
    except StaleElementReferenceException:
        pass

其中 number_of_elements 代表页面包含的元素数量。然后,使用 WebdriverWait.until 获取元素

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

def get_elements(driver):
    wait = WebDriverWait(driver,10)
    return wait.until(ec.presence_of_all_elements_located((By.XPATH,path_to_element))

并将函数传递给wait_until_all_expected_elements,如下所示:

elements = wait_until_all_expected_elements(lambda: get_elements(driver),number_of_elements)
,

我在网页抓取方面遇到了同样的问题。尝试使用 python 内置的 time 库。您只需输入 time.sleep(number_of_seconds),网站就会有时间加载,然后您就可以查找所需内容。

import time

driver.get(your_website_here)

time.sleep(5)  # Wait 5 seconds for page to fully load

driver.find_elements_by_class("abcd")

,

我的问题是:该站点在浏览器中未处于焦点时不会加载块。 目标是 - 滚动到您需要的所有 div:

         self.driver.get(url_)
         product_elements = self.driver.find_elements_by_class_name("product-cards")
 
         for elm in product_elements:
             elm.location_once_scrolled_into_view

关于如何使用Selenium WebDriver单击列表中的特定元素?selenium单选按钮的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于css – 如何使用Selenium WebDriver,Java按文本选择Web元素、Java 如何强制Selenium WebDriver单击当前不可见的元素?、ruby – 无法使用Selenium WebDriver单击Safari中的按钮、Selenium WebDriver WebDriverWait如何等待一组相同的元素?等相关内容,可以在本站寻找。

本文标签:

上一篇通过execute_script创建元素时无法找到find_element_by_id

下一篇使用python selenium chromedriver从源中选择隐藏的选项值(selenium获取隐藏元素)