我尝试通过滚动到元素来单击隐藏的按钮,但仍然无法正常工作。仅当我手动滚动它时,编码才起作用。我可以知道原因吗?谢谢! 网站:https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
driver = webdriver.Chrome("C:/Users/Ivan.Chak/Desktop/cbbc/chromedriver", chrome_options=options)
driver.implicitly_wait(30)
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
driver.find_element_by_css_selector("#tier1-select .combobox-input-wrap").click()
driver.find_element_by_css_selector(".searchType .droplist-item:nth-child(2) > a").click()
driver.find_element_by_css_selector("#rbAfter2006 .combobox-field").click()
element = driver.find_element_by_xpath("//a[contains(text(),'Debt and Structured Products')]")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
time.sleep(1)
element.click() <--- This is not working!!!
---------------------------------------------------------------------------
ElementNotInteractableException Traceback (most recent call last)
<ipython-input-323-cf25f85114ab> in <module>()
----> 1 element.click()
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
631 params = {}
632 params['id'] = self._id
--> 633 return self._parent.execute(command, params)
634
635 def find_element(self, by=By.ID, value=None):
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=75.0.3770.142)
答案 0 :(得分:0)
您可能会滚动到element(button)。如果您的网页具有任何功能区,那么这又将被隐藏。尝试滚动到其上方的任何其他元素。这解决了我的问题。
答案 1 :(得分:0)
您必须先滚动到每个菜单项,然后才能使用JavaScript arguments[0].scrollIntoView(true);
您可以使用常规方法进行滚动,然后再单击下面的代码,就像下面的代码一样:
def click(element):
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
driver = webdriver.Chrome()
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
driver.find_element_by_css_selector(".searchType").click()
driver.find_element_by_xpath("//div[@class='droplist-item' and ./a[.='Headline Category']]").click()
driver.find_element_by_id("rbAfter2006").click()
click(driver.find_element_by_xpath("//a[.='Debt and Structured Products']"))
click(driver.find_element_by_xpath("//a[.='Debt Securities']"))
click(driver.find_element_by_xpath("//a[.='Issuer-Specific Report - Debt Securities']"))
或者您可以使用如下所示的菜单选择特定方法:
def select_menu(menu_text):
element = driver.find_element_by_xpath(f"//a[.='{menu_text}']")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
driver = webdriver.Chrome()
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
driver.find_element_by_css_selector(".searchType").click()
driver.find_element_by_xpath("//div[@class='droplist-item' and ./a[.='Headline Category']]").click()
driver.find_element_by_id("rbAfter2006").click()
select_menu("Debt and Structured Products")
select_menu("Debt Securities")
select_menu("Issuer-Specific Report - Debt Securities")
选择搜索条件的更通用的方法:
def select_search_criteria(criteria, *menus):
driver.find_element_by_css_selector(".searchType").click()
criteria_element = driver.find_element_by_xpath(f"//div[@class='droplist-item' and ./a[.='{criteria}']]")
data_value = criteria_element.get_attribute("data-value")
criteria_element.click()
document_type_element = driver.find_element_by_id(data_value)
if document_type_element.find_element_by_css_selector("a.combobox-field").get_attribute("aria-expanded") == "false":
document_type_element.click()
for menu in menus:
element = document_type_element.find_element_by_xpath(f".//a[.='{menu}']")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
driver = webdriver.Chrome()
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
select_search_type("Headline Category", "Debt and Structured Products", "Debt Securities")
select_search_type("Document Type", "Circulars")