Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我无法使用selenium单击下拉元素_Selenium_Automation_Dropdown - Fatal编程技术网

我无法使用selenium单击下拉元素

我无法使用selenium单击下拉元素,selenium,automation,dropdown,Selenium,Automation,Dropdown,我想点击“Mon profil” 我试过: driver.findElement(By.className("profile-navbar-label dropdown-item")).click() 及 它不起作用 试试这个Xpath driver.findElement(By.xpath("//button[contains(.,'Mon profil')]")).click() 或 不能使用带空格的类名来查找元素。如果你想使用这样的类名,你必须为此编写xpath。所以第一种查找方法是

我想点击“Mon profil” 我试过:

driver.findElement(By.className("profile-navbar-label dropdown-item")).click()

它不起作用


试试这个
Xpath

driver.findElement(By.xpath("//button[contains(.,'Mon profil')]")).click() 


不能使用带空格的类名来查找元素。如果你想使用这样的类名,你必须为此编写xpath。所以第一种查找方法是错误的,关于
xpath
我不确定这是否是写xpath的正确方法

试试这个xpath=
(.//button[@type='button'])[1]
它会工作的


通过单击F12,然后单击
ctrl+F
,可以检查
xpath是否找到了正确的web元素。输入
xpath
它将突出显示webelement。

首先您应该单击选择框,当显示下拉列表时,使用以下代码

driver.findElement(By.xpath("//button[text()='Mon profil']")).click() 


请不要将代码作为截图。您可以从浏览器调试控制台将代码复制并粘贴到问题中。阅读截图中的代码太难了。
driver.findElement(By.xpath("//button[text()='Mon profil']")).click() 
driver.findElement(By.xpath("//button[text()='Mon profil']")).click() 
WebElement = driver.findElement(By.xpath("//button[text()='Mon profil']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", webElement);