Python selenium web驱动程序访问列表框值
我在访问列表框值时遇到了Selenium web驱动程序的问题 检查图像: 我可以粘贴值并传递我想要选择的列表的类名,基本上每次我都要选择列表中显示的第一个选项Python selenium web驱动程序访问列表框值,python,selenium,selenium-webdriver,python-3.5,Python,Selenium,Selenium Webdriver,Python 3.5,我在访问列表框值时遇到了Selenium web驱动程序的问题 检查图像: 我可以粘贴值并传递我想要选择的列表的类名,基本上每次我都要选择列表中显示的第一个选项 1 driver.get('https://my.maerskline.com/schedules/vessel') 2 button = driver.find_element_by_id("s2id_b-vesselCode") 3 button.click() 4 button1
1 driver.get('https://my.maerskline.com/schedules/vessel')
2 button = driver.find_element_by_id("s2id_b-vesselCode")
3 button.click()
4 button1 = driver.find_element_by_id("s2id_autogen1_search")
5 button1.send_keys(Vessel_name)
6 button3 = driver.find_element_by_id("select2-results-dept-0 select2-result select2-result-selectable")
7 button3.click()
8 button2 = driver.find_element_by_id("schedulesByVesselSearchButton")
button2.click()
第6行给出了下面的错误
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
{"method":"id","selector":"select2-results-dept-0 select2-result
select2-result-selectable"} (Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64)
你知道我该怎么做吗?如果需要更多信息,请告诉我。谢谢
更新代码
if Vessel_name != "":
#driver.get('https://my.maerskline.com/vessels?searchTerm={0}'.format(Vessel_name))
print(Vessel_name)
driver.get('https://my.maerskline.com/schedules/vessel')
button = driver.find_element_by_id("s2id_b-vesselCode")
button.click()
button1 = driver.find_element_by_id("s2id_autogen1_search")
button1.send_keys(Vessel_name)
button3 =driver.find_element_by_css_selector("#select2-results-1>li:nth-child(1)")
button3.click()
#button2 = driver.find_element_by_id("schedulesByVesselSearchButton")
#button2.click()
try:
soup = BeautifulSoup(driver.page_source)
您应该通过元素的类名来定位元素。您提供的映像中的元素没有任何id 试一试:
//ul[@id='select2-results-1']/li[@class='select2-results-dept-0 select2-result select2-result-selectable select2-highlighted']
如果确定始终要选择第一个li
,则可以通过引用其索引使xpath更加整洁:
(//ul[@id='select2-results-1']/li)[1]
因为这是带有值的下拉列表,所以用户应该选择类来标识web元素 您可以参考以下链接来解决此问题:-
您正在调用
按id查找元素,但给出了类名。您应该使用element\u by\u css\u选择器(“select2-results-1>li:nth child(2)”)
作为第二个选项
"#select2-results-1 > li:nth-child(1)" >> MAJESTIC
"#select2-results-1 > li:nth-child(2)" >> MAJESTC MAERSK
"#select2-results-1 > li:nth-child(3)" >> NORTHERN MAJESTC
检查以下各项
driver.get('https://my.maerskline.com/schedules/vessel')
button = driver.find_element_by_id("s2id_b-vesselCode")
button.click()
button1 = driver.find_element_by_id("s2id_autogen1_search")
button1.send_keys(Vessel_name)
button3 = driver.find_element_by_css_selector("#select2-results-1 > li:nth-child(2)")
button3.click()
button2 = driver.find_element_by_id("schedulesByVesselSearchButton")
button2.click()
检查此代码是否有效:
>>> driver.button = driver.find_element_by_id("s2id_b-vesselCode")
>>> button = driver.find_element_by_id("s2id_b-vesselCode")
>>> button.click()
>>> button1 = driver.find_element_by_id("s2id_autogen1_search")
>>> button1.send_keys(Vessel_name)
>>> button1.send_keys("majestic")
>>> button3 = driver.find_element_by_css_selector("#select2-results-1 > li:nth-child(2)")
>>> button3.click()
>>> button2 = driver.find_element_by_id("schedulesByVesselSearchButton")
>>> button2.click()
>>> driver.find_element_by_css_selector(".muted.scheduleVesselHeader").text
u'Port Terminal Arrival Voyage Departure'
>>> driver.find_element_by_css_selector("tbody > tr:nth-child(2)").text
u'Rotterdam\nAPM 2 Terminal Maasvlakte II 15 Jan 2017, 06:00 649W | 649W 17 Jan 2017, 05:00'
抱歉-我仅按类名搜索,错误信息为:选择器无效:不允许使用复合类名(会话信息:chrome=55.0.2883.87)(驱动程序信息:chromedriver=2.26.436362(5476ec6bf7ccbada1734a0cdec7d570bb042aa30),平台=Windows NT 6.1.7601 SP1 x86_64),如果为元素分配了多个类名,在selenium中不能使用类名集合。这称为复合类名。因此,您需要更具体地使用定位器。xpath或css更适合这里。很酷,如果你卡住了。在这里发布你的尝试,我会帮助你。button3=driver。通过xpath(“div[@id='select2-drop']/ul[@id='select2-results-1']/li[@class='select2-results-dept-0 select2 result select2 result select2 result select2 result select2 highted'])查找元素。我们可以吗?我不这么认为,因为在我的HTML中没有选项,也没有使用select标记,只是表示歉意-我只按类名搜索,错误是-Er Gaurav Chhabra 52分钟前消息:无效选择器:不允许使用复合类名(会话信息:chrome=55.0.2883.87)(驱动程序信息:chromedriver=2.26.436362(5476ec6bf7ccbada1734a0cdec7d570bb042aa30),平台=Windows NT 6.1.7601 SP1 x8664)现在我尝试使用xpath,因为没有其他方法可以解决这个问题。你尝试过答案吗?我尝试过类和id,得到了相同的错误-selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:找不到元素:另外,如果我在html中传递这个参数,我们有多个同名的类,这不是会产生问题吗E