How to select an option from a dropdown that wasn't built with the 'select' method/tag? (Selenium Java)

huangapple 未分类评论78阅读模式
英文:

How to select an option from a dropdown that wasn't built with the 'select' method/tag? (Selenium Java)

问题

# 代码部分不翻译,以下为翻译的内容:

我正在尝试从下拉菜单中选择选项但它没有 'select' 标签所以如果我尝试在 Selenium 中使用 'select' 函数它不起作用然后我尝试按照下面的代码示例执行点击操作和交互我必须以这种方式进行点击因为网站中有一条消息不允许我以正常方式点击对象),但它不起作用代码运行时不显示错误但也不会选择任何选项

WebElement ele1 = driver.findElement(By.xpath("//*[text()=' - Select a Service - ']"));
JavascriptExecutor executor1 = (JavascriptExecutor) driver;
executor1.executeScript("arguments[0].click();", ele1);

Actions keyDown = new Actions(driver);
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN)).perform();

我的代码有什么问题吗或者也许你可以告诉我另一种在下拉菜单没有 'select' 标签时选择选项的方法

**注意** 下面是下拉菜单代码的图像它是带有数字 '1' 的括号中的菜单唯一与另一个下拉菜单区分的是标题红色矩形内的文本)。

[![在这里输入图片描述][1]][1]
英文:

I'm trying to select options from a dropdown, but it doesn't have the 'select' tag, so if I try to use the 'select' function in selenium it doesn't work. Then I tried following an example about clicking and using interactions like in the code below (I had to do the clicking that way because there's a message in the website that doesn't let me click objects in the normal way) but it doesn't work, the code runs, shows no errors, but doesn't select any option either.

WebElement ele1 = driver.findElement(By.xpath("//*[text()='- Select a Service -']"));
JavascriptExecutor executor1 = (JavascriptExecutor)driver;
executor1.executeScript("arguments[0].click();", ele1);
       	            
Actions keyDown = new Actions(driver);
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN)).perform();

Is there something wrong in my code? Or maybe you could tell me another way of selecting options from dropdowns when they don't have the 'select' tag.

Note: Below is an image of the dropdown code, it's the one in the bracket with the number '1'. The only thing to differentiate it from another dropdown is the title (the text within the red rectangle).

How to select an option from a dropdown that wasn't built with the 'select' method/tag? (Selenium Java)

huangapple
  • 本文由 发表于 2020年8月15日 02:46:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63418471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定