python - 元素在点(x,y)处不可交互/不可点击
问题描述
我试图使用 Selenium 和 chromedriver 从https://edm.com/news的多个页面中抓取文章,并在尝试点击“查看更多”按钮时遇到多个错误。关于我可以尝试什么的任何想法?
我尝试过使用 ActionChains.move_to_element(..).click.perform() 也尝试过多次 time.sleep 调用或 WebDriverWait.until ... 似乎没有任何效果。
start_url = "https://edm.com/news"
browser = webdriver.Chrome(executable_path='chromedriver.exe', options=self.option)
browser.get(self.start_url)
# Wait max 10 secs for page to load
timeout = 10
WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="lyra-wrapper"]/div/div[3]/section/'
'div[2]/section[2]/section/div/button')))
time.sleep(2)
button = browser.find_elements(By.XPATH, '//*[@id="lyra-wrapper"]/div/div[3]/section/'
'div[2]/section[2]/section/div/button')[0]
button.click()
File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (508, 4270)
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.17763 x86_64)```
解决方案
尝试通过文本查找元素"See More"
start_url = "https://edm.com/news"
browser = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
browser.get(start_url)
# Wait max 10 secs for page to load
wait = WebDriverWait(browser, 10)
time.sleep(2)
close_advert = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="lyra-wrapper"]/div/div[4]/phoenix-ad[2]/div/div[2]')))
close_advert.click()
see_more = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(.,"See More")]')))
time.sleep(2)
try:
see_more.click()
except:
time.sleep(3)
see_more.click()
推荐阅读
- javascript - Vanilla JS ForEach 元素并分配升序变量
- asp.net - 如何在 asp.net core 3.1 中使用多个 Id 执行路由
- python-3.x - 从熊猫数据框中提取特定信息
- oracle-apex - 如何根据 Oracle Apex 表单中一个数字字段中的值创建多个字段?
- java - 如何解决 java.nio.file.AccessDeniedException 以读取 .sys 文件
- postgresql - 如何查询numrange字段的值?
- google-api - “测试安装流程”/“与 Google 集成”按钮缺失
- java - 如何从 Firebase RealTime 数据库中获取用户列表?
- java - 如何将消息从 X-DEAD-LETTERS 队列重新发送到另一个队列?
- swift - 如何在 Swift 上发出多个本地通知?