GVKun编程网logo

如何从使用Selenium Webdriver中的api响应动态启动的下拉列表中选择特定位置?

9

在本文中,我们将为您详细介绍如何从使用SeleniumWebdriver中的api响应动态启动的下拉列表中选择特定位置?的相关知识,此外,我们还会提供一些关于css–如何使用SeleniumWebDr

在本文中,我们将为您详细介绍如何从使用Selenium Webdriver中的api响应动态启动的下拉列表中选择特定位置?的相关知识,此外,我们还会提供一些关于css – 如何使用Selenium WebDriver,Java按文本选择Web元素、java – 在Mac上的Safari浏览器10上使用Selenium Webdriver无法选择下拉列表、Selenium WebDriver-操作页面下拉列表、Selenium WebDriver:使用WebDriver.findElement定位时,等待元素出现的有用信息。

本文目录一览:

如何从使用Selenium Webdriver中的api响应动态启动的下拉列表中选择特定位置?

如何从使用Selenium Webdriver中的api响应动态启动的下拉列表中选择特定位置?

问题描述:

我正在尝试自动填写有地区下拉列表的表格。我在选择建议列表中建议的选项时遇到问题。建议列表中的选项由api响应(即动态)提供。我无法从建议的列表中选择选项之一。

String Locality ="//label[contains(text(),''Locality'')]/following-sibling::input";public void insertData(DataTable str) throws InterruptedException {    List<List<String>> list = str.asLists(String.class);    super.identifyElement(locators.xpath, Locality), list.get(1).get(5));// value sendkey= Akurdi;

HTML元素:

<label _ngcontent-c4="" for="Location">Locality</label><input _ngcontent-c4="" autocapitalize="off" autocorrect="off"formcontrolname="locality" placeholder="" spellcheck="off" stype="locality" type="text" ng-reflect-klass="mb-0" ng-reflect-ng-ng-reflect-name="locality" ng-reflect-model="Akurdi" autocomplete="off">

Sendkey值形式,黄瓜脚本:Akurdi … 显示输入下拉列表UI

答案1

小编典典

在这些情况下,我将像通常的List一样使用这些下拉菜单。单击下拉菜单,等待下拉菜单出现,然后在其元素列表中进行迭代,按名称搜索我需要的内容,然后单击它。

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 – 在Mac上的Safari浏览器10上使用Selenium Webdriver无法选择下拉列表

java – 在Mac上的Safari浏览器10上使用Selenium Webdriver无法选择下拉列表

在Safari浏览器中,我需要从下拉列表中选择一个选项.
以下代码适用于除Mac OS上的Safari之外的所有浏览器.我使用的是Safari 10.1.1和selenium web驱动程序版本3.3.1我用 Java编写了代码.请参阅以下代码 –

webElement = findElement(field);
if (webElement.isdisplayed())
{
  Select select = new Select(webElement);
  select.selectByVisibleText(value);
}

解决方法

你可以试试这段代码:

public void jsSelect(WebElement element,int index) {
        JavascriptExecutor executor = (JavascriptExecutor) driver;
        executor.executeScript("arguments[0].selectedindex=" + index + ";",element);
    }

public void jsSelect(WebElement element,String item) {
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("const textToFind = '" + item + "';" +
            "const dd = arguments[0];" +
            "dd.selectedindex = [...dd.options].findindex (option => option.text === textToFind);",element);
}

Selenium WebDriver-操作页面下拉列表

Selenium WebDriver-操作页面下拉列表

#encoding=utf-8
import unittest
import time
import chardet
from selenium import webdriver
 
class VisitSogouByIE(unittest.TestCase):

    def setUp(self):
        #启动IE浏览器
        #self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
        self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
        
    def test_printSelectText(self):
        url = "http://127.0.0.1/test_select.html"
        # 访问自定义的html网页
        self.driver.get(url)
        # 使用name属性找到页面上name属性为“fruit”的下拉列表元素
        select = self.driver.find_element_by_name("fruit")
        all_options = select.find_elements_by_tag_name("option")
        for option in all_options:
            print u"选项显示的文本:", option.text
            print u"选项值为:", option.get_attribute("value")
            option.click()
            time.sleep(1)

       



    def tearDown(self):
        # 退出IE浏览器
        self.driver.quit()

if __name__ == ''__main__'':
    unittest.main()

 

Selenium WebDriver:使用WebDriver.findElement定位时,等待元素出现

Selenium WebDriver:使用WebDriver.findElement定位时,等待元素出现

等待和WebElement一起出现很方便。WebDriverWait``ExpectedConditions

问题是, 如果 找到元素的唯一可能方法
什么WebElement.findElment,因为它没有id,没有名称,没有唯一的类?

WebDriverWait的构造函数仅接受WebDriver作为参数,而不接受WebElement

我已经设定了implicitlyWait时间,所以使用它似乎不是一个好主意try{} catch(NoSuchElementException e){},因为我不想为这个元素等待那么长时间。

这是场景:

有一个网页,其中包含许多input标签。每个input标签都有格式要求。

当不满足格式要求时,动态div标签将出现在该input标签之后。

由于input标签太多,因此我创建了一个通用方法,例如:

public WebElement txtBox(String name) {
    return driver.findElement(By.name(name));
}

而不是为每个input标签创建数据成员。

然后,我创建一种方法isValid来检查某些用户输入是否input有效。我要做的isValid就是检查div后面是否有标记inputboxToCheck,其代码如下:

public boolean isValid(WebElement inputboxToCheck) {
    WebElementWait wait = new WebElementWait(inputboxToCheck,1);
    try {
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("./following-sibling::div")));
        return false;
    } catch (TimeOutException e) {
        return true;
    }    
}

WebElementWait是一个虚构(不存在)的类,其工作方式与相同WebDriverWait

今天关于如何从使用Selenium Webdriver中的api响应动态启动的下拉列表中选择特定位置?的介绍到此结束,谢谢您的阅读,有关css – 如何使用Selenium WebDriver,Java按文本选择Web元素、java – 在Mac上的Safari浏览器10上使用Selenium Webdriver无法选择下拉列表、Selenium WebDriver-操作页面下拉列表、Selenium WebDriver:使用WebDriver.findElement定位时,等待元素出现等更多相关知识的信息可以在本站进行查询。

本文标签: