Javascript 单击';onclick';使用selenium和Python的按钮
我需要从中删除一些信息 但是,该页面在进入之前需要年龄确认。它有一个按钮,必须单击该按钮才能看到页面。我的问题是我点击这个按钮的方法根本不起作用 我正在使用selenium和Python 2.7.10 这是我的密码:Javascript 单击';onclick';使用selenium和Python的按钮,javascript,python,selenium,xpath,Javascript,Python,Selenium,Xpath,我需要从中删除一些信息 但是,该页面在进入之前需要年龄确认。它有一个按钮,必须单击该按钮才能看到页面。我的问题是我点击这个按钮的方法根本不起作用 我正在使用selenium和Python 2.7.10 这是我的密码: def download_specific_page(url): try: browser = get_browser() wait = WebDriverWait(browser, 30) browser.get(main_
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()
不,我使用Firefox,但它可以工作,谢谢:)(我犯了一个愚蠢的错误,我不使用Chromium;)当我运行这个示例时,我使用了Linux。现在在Windows上尝试,我收到消息
无法使用css选择器“close”找到元素
@darias I updated answer,这可能是一个等待问题-我添加了一个隐式等待,并确保窗口正在出现。。。