Selenium为什么不能将“click”应用于弹出窗口?

huangapple go评论65阅读模式
英文:

Why is selenium not applying "click" to a popup?

问题

我知道这个弹出标签元素的XPath是正确的,但是当我执行filters_language_dropdown.click()然后.send_keys(Keys.Enter)时,它什么也不做。

然而,同一个弹出标签(在此页面上点击“筛选”按钮以查看)可以使用初始按钮按压元素的XPath(见下面的代码和图片)工作,所以可以使用filters_button.send_keys...。发生了什么?

Selenium为什么不能将“click”应用于弹出窗口? Selenium为什么不能将“click”应用于弹出窗口?

初始化浏览器并导航到页面

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")

(在正常工作状态下):查找关键字,使其可点击,清除框中的现有数据,输入新信息,保持页面打开10秒

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)

激活筛选条件(英文,活跃广告,日期从(最近2天)到今天)

filters_button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//body1/div1/div1/div1/div1/div1/div1/div[5]/div2/div1/div1/div3/div1/div1/div1/div1/div1/div1/div1")))
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()
filters_language_dropdown.send_keys(Keys.ENTER)
time.sleep(3)

英文:

I know the xpath of this popup tabs element is correct, however when I do filters_language_dropdown.click() and then .send_keys(Keys.Enter. It doesn't do anything.

However the same popup (press 'filters' button on this page to view) works with the xpath of the initial button press element instead (see code + images below) so with filters_button.send_keys.... Whats going on?

Selenium为什么不能将“click”应用于弹出窗口? Selenium为什么不能将“click”应用于弹出窗口?

        # Initialize the browser and navigate to the page
        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()
        filters_language_dropdown.send_keys(Keys.ENTER)
        time.sleep(3)

答案1

得分: 1

使用以下xpath来点击筛选,然后点击所有语言,然后点击英语,如果要更改其他语言,您需要将English更改为French,例如。

代码:

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")

search = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='Search by keyword or advertiser']")))
search.click()
search.clear()
search.send_keys(" " + Keys.ENTER)

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Filters']"))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='All languages']"))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='English']")).click()
time.sleep(10) # 用于检查操作

浏览器快照:(图片链接未包含在翻译中)

英文:

Use following xpath to click on the filter and then click on all languages and then click on English if you want to change other language you need to pass let say 'French',you need to change instead of English.

Code:

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")

search=WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//input[@placeholder='Search by keyword or advertiser']")))
search.click()
search.clear()
search.send_keys("" "" + Keys.ENTER)

WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='Filters']"))).click()
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='All languages']"))).click()
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='English']"))).click()
time.sleep(10) # to check the operation

Browser snapshot:

Selenium为什么不能将“click”应用于弹出窗口?

huangapple
  • 本文由 发表于 2023年2月8日 20:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定