我正试图从这个文件调优动态网站中抓取数据,该网站通过javascript(ajax(请求加载数据。
我想做的是,它从类型中选择汽车,然后迭代地选择品牌、型号和发动机,然后我想为每个品牌、模型和发动机收集数据。
这是我编写的代码,用于从类型中选择汽车
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
PATH = "C:SeleniumDriversgeckodriver.exe"
driver = webdriver.Firefox(executable_path=PATH)
driver.get("https://file-tuning.com/chiptuning")
type_element_select = driver.find_element_by_id("type")
action = ActionChains(driver)
action.move_to_element(type_element_select)
action.click(type_element_select)
action.perform()
action.move_to_element(Select(type_element_select).select_by_value("cars"))
action.click(Select(type_element_select).select_by_value("cars"))
action.perform()
我得到的错误:
Traceback (most recent call last):
File "D:Pythonseleniumtest.py", line 27, in <module>
action.move_to_element(Select(type_element_select).select_by_value("cars"))
File "C:UsersUmairAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriversupportselect.py", line 82, in select_by_value
self._setSelected(opt)
File "C:UsersUmairAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriversupportselect.py", line 212, in _setSelected
option.click()
File "C:UsersUmairAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:UsersUmairAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:UsersUmairAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersUmairAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view
我也尝试过这个
types_element = driver.find_element_by_id("type")
types_object = Select(types_element)
types_object.select_by_visible_text("Cars")
但它也给了我同样的例外。我该如何浏览这个网站?已经看到并尝试了其他StackOverflow相关的问题,但没有为我解决。
您需要在全屏模式下使用浏览器:
driver.maximize_window()
以便Selenium可以看到所有的web元素。
示例代码:
PATH = "C:SeleniumDriversgeckodriver.exe"
driver = webdriver.Firefox(executable_path=PATH)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://file-tuning.com/chiptuning")
wait = WebDriverWait(driver, 10)
type_element_select = driver.find_element_by_id("type")
action = ActionChains(driver)
action.move_to_element(type_element_select)
action.click(type_element_select)
action.perform()
action.move_to_element(Select(type_element_select).select_by_value("cars"))
action.click(Select(type_element_select).select_by_value("cars"))
action.perform()