Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/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
Javascript 如何使用selenium选择没有id的下拉菜单_Javascript_Python_Html_Selenium - Fatal编程技术网

Javascript 如何使用selenium选择没有id的下拉菜单

Javascript 如何使用selenium选择没有id的下拉菜单,javascript,python,html,selenium,Javascript,Python,Html,Selenium,我曾尝试通过CSS选择器选择一个页面上没有id的下拉菜单,但我无法让它工作。以下是下拉代码: <select style="margin: 5px auto; width: 146px;" onchange="document.getElementById('11qq').src=this.options[this.selectedIndex].value;"> <option value="https://player.vimeo.com/video/158733095"&g

我曾尝试通过CSS选择器选择一个页面上没有id的下拉菜单,但我无法让它工作。以下是下拉代码:

<select style="margin: 5px auto; width: 146px;" onchange="document.getElementById('11qq').src=this.options[this.selectedIndex].value;">
<option value="https://player.vimeo.com/video/158733095">Shakedown</option>
<option value="x">Placeholder</option>
<option value="https://player.vimeo.com/video/158815551">Race</option>
</select>
我从下面的第二个答案中得到的Select droplist部分。但是,我得到以下错误:

Select droplist = new Select(driver.findElement(By.CSS_SELECTOR("select")));
                  ^
SyntaxError: invalid syntax
您可以尝试使用onchange属性

driver.find_element_by_css_selector('onchange*="document.getElementById('11qq')"')
这将为您提供包含document.getElementById'11qq'的具有onchange属性的元素。

您可以尝试使用onchange属性

driver.find_element_by_css_selector('onchange*="document.getElementById('11qq')"')

这将为您提供包含document.getElementById'11qq'的具有onchange属性的元素。

也许您在python中使用java方法。 以下是Java方法:

Select droplist = new Select(driver.findElement(By.CSS_SELECTOR("select")));
以下是Python方法:

droplist = driver.find_element_by_css_selector('select')

也许您在python中使用java方法。 以下是Java方法:

Select droplist = new Select(driver.findElement(By.CSS_SELECTOR("select")));
以下是Python方法:

droplist = driver.find_element_by_css_selector('select')

我将使用项目的值来定位下拉列表。以下是您的页面的工作示例:

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

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

driver.get("http://racing4everyone.eu/2016/03/12/formula-e-201516-round05-mexico/")

for x in ["Shakedown", "Race"]:
  # select the option
  Select(driver.find_element_by_xpath("//select[option='" + x + "']")).select_by_visible_text(x)

  # set context on the video frame
  frame_video = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*=video]")))
  driver.switch_to.frame(frame_video)

  # set the default context
  driver.switch_to_default_content()

driver.quit()

请注意,第二个项目占位符没有链接。

我将使用项目的值来定位下拉列表。以下是您的页面的工作示例:

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

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

driver.get("http://racing4everyone.eu/2016/03/12/formula-e-201516-round05-mexico/")

for x in ["Shakedown", "Race"]:
  # select the option
  Select(driver.find_element_by_xpath("//select[option='" + x + "']")).select_by_visible_text(x)

  # set context on the video frame
  frame_video = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*=video]")))
  driver.switch_to.frame(frame_video)

  # set the default context
  driver.switch_to_default_content()

driver.quit()

请注意,第二个项目占位符没有链接。

droplist.selectByVisibleTextx似乎也无效,Python对此的等价物是什么?正如“Florent,B”所说,正如“Florent,B”所说,您可以引用以下方法:Selectdriver.find_element_by_xpath//select[option='+x+'].select_by_visible_textx下面是Python的API:我希望它能帮助你。正如您所看到的,Java和python在命名约定上是不同的。例:1。getName//Java2.get_name Python必须选择一个如下所示的Web元素。。。由于某些原因,它不适用于By.tagNameselect,但适用于cssSelector。我不知道,但还是要感谢你的回答:droplist.selectByVisibleTextx似乎也无效,Python的等价物是什么?正如“Florent,B”所说,正如“Florent,B”所说,你可以引用以下方法:Selectdriver.find_element_by_xpath//select[option='+x+'].select_by_visible_textx下面是Python的API:我希望它能帮助你。正如您所看到的,Java和python在命名约定上是不同的。例:1。getName//Java2.get_name Python必须选择一个如下所示的Web元素。。。由于某些原因,它不适用于By.tagNameselect,但适用于cssSelector。我不知道,但还是要感谢你的回答:嗯,有没有办法检查视频的存在,如果视频不存在:继续循环中的下一个x?很遗憾,我无法手动输入x值。要检查视频是否存在,只需在值以http开头的选项元素上循环。无需手动输入x,这只是一个示例,演示了在您的案例中使用select,这就是问题所在。嗯,是否有方法检查视频是否存在,如果视频不存在:继续循环中的下一个x?很遗憾,我无法手动输入x值。要检查视频是否存在,只需在值以http开头的选项元素上循环。无需手动输入x,这只是一个示例,演示了在您的案例中选择的用法,这就是问题所在。