2022年 11月 5日

python 点击按钮 click_用selenium和Python单击“onclick”按钮

我需要从这个page中删除一些信息。在

但是,页面在进入之前需要确认年龄。它有一个按钮,必须单击该按钮才能看到页面。我的问题是我点击这个按钮的方法根本不起作用。在

我正在使用selenium和Python2.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,但仍然没有成功:

^{pr2}$

有什么办法点击这个按钮,这样我就可以废掉这个页面了吗?在