![](/img/trans.png)
[英]Python + Selenium — unable to click button (onClick=openWIndow)
[英]Click 'onclick' button with selenium and Python
我需要从此页面抓取一些信息。
但是,该页面需要年龄确认才能输入。 它具有一个按钮,在查看该页面之前必须先单击该按钮。 我的问题是我单击此按钮的方法根本行不通。
我正在使用selenium和Python 2.7.10。
这是我的代码:
def download_specific_page(url):
try:
browser = get_browser()
wait = WebDriverWait(browser, 30)
browser.get(main_page_url)
time.sleep(2)
buttons = browser.find_elements_by_class('close')
for button in buttons:
onclick_text = button.get_attribute('onclick')
if onclick_text and re.search('ConfirmAge();', onclick_text):
print "found it!"
button.click()
browser.get(url)
html = browser.page_source
browser.close()
return html
except Exception:
info = 'Generic exception\n'
return ""
我也尝试使用xpath,但仍然没有成功:
def download_specific_page(url):
try:
browser = get_browser()
wait = WebDriverWait(browser, 30)
browser.get(main_page_url)
button = browser.find_element_by_xpath("/html/body/div#bg_image/div.container/div#content/div#confirmage/div.confirmage_navigation/button.close[1]")
button.click()
time.sleep(2)
browser.get(url)
html = browser.page_source
browser.close()
return html
except Exception:
info = 'Generic exception\n'
return ""
有什么想法如何单击此按钮以便我可以抓取页面吗?
使用常规点击:
from selenium import webdriver
URL = 'https://www.24dolores.pl/pl/ogloszenia-towarzyskie'
CSS_SELECTOR = '.close'
browser = webdriver.Chrome()
browser.implicitly_wait(10)
browser.get(URL)
close = browser.find_element_by_css_selector(CSS_SELECTOR)
close.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.