我正在使用Selenium和python进行网页抓取,以及用于测试此链接的页面
但是问题是我无法处理下拉菜单的动态内容,这是出现的问题
在选择州时,根据州来加载城市,据我所知,一些Php和js在后端运行。
因此,我在网上搜索了解决方案,等待一段时间,请使用此链接作为参考。
以下是我的代码的一部分
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chrome_path = r"E:\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("http://www.blooddonors.in")
select = Select(driver.find_element_by_xpath('/html/body/table[3]/tbody/tr/td[2]/table[1]/tbody/tr/td/form/table/tbody/tr[2]/td[1]/select'))
select.select_by_visible_text('Tamil Nadu')
driver.implicitly_wait(60)
drop = Select(driver.find_element_by_xpath('//*[@id="div_city"]/select'))
select.select_by_visible_text('Coimbotore')
我正在使用Windows sys,并且尝试使用CMD。它不需要等待功能,没有它就可以正常工作。
我面临的错误是:
raise NoSuchElementException("Could not locate element with visible text: %s" % text)
selenium.common.exceptions.NoSuchElementException: Message: Could not locate element with visible text: Coimbotore
但是他们实际上是他们的。
如果有人可以帮助我解决问题,那就太好了,我可以继续进行下一个。
谢谢
要选择Tamilnadu
然后选择,Coimbotore
您可以使用以下代码块:
driver.get("http://www.blooddonors.in")
select = Select(driver.find_element_by_name('select'))
select.select_by_visible_text('Tamil Nadu')
drop = Select(driver.find_element_by_name('city'))
city_option = WebDriverWait(driver, 5).until(lambda x: x.find_element_by_xpath("//select[@name='city']/option[text()='Coimbotore']"))
city_option.click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句