提前道歉,不知道如何在不显示代码和图像的情况下更好地表达这个问题。
在下面的selenium脚本中,我成功地单击了网站页面上的button
元素(过滤器),但是当它弹出时,我尝试单击下拉元素;使用相对xpath(正如我对button元素所做的那样)没有任何作用。
更奇怪的是,之前用于单击(过滤器)按钮的变量,以某种方式激活了下拉元素。。。发生什么事?我想知道为什么会这样。
初始化浏览器并导航到页面
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install())) browser.get("https://www.facebook.com/ads/library/?active_status=all&ad_type=all&country=ALL&q=%22%20%22&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_exact_phrase&media_type=all") # (In working order): Look for keyword, make it clickable, clear existing data in box, enter new info, keep page open for 10 seconds search_box = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search by keyword or advertiser']"))) search_box.click() search_box.clear() search_box.send_keys("" "" + Keys.ENTER) time.sleep(3) # Activating the filters (English, active ads, date from (last 2 days) to today) filters_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[5]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]"))) filters_button.click() filters_button.send_keys(Keys.ENTER) time.sleep(3) filters_language_dropdown = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='js_knd']//div[@class='x6s0dn4 x78zum5 x13fuv20 xu3j5b3 x1q0q8m5 x26u7qi x178xt8z xm81vs4 xso031l xy80clv xb9moi8 xfth1om x21b0me xmls85d xhk9q7s x1otrzb0 x1i1ezom x1o6z2jb x1gzqxud x108nfp6 xm7lytj x1ykpatu xlu9dua x1w2lkzu']"))) filters_language_dropdown.click() #_______if i switch this to filters_button instead, it works_____why? filters_language_dropdown.send_keys(Keys.ENTER) time.sleep(3)