1

我正在尝试从谷歌获取一些数据,但我得到了“在你继续之前”谷歌弹出窗口。我试图让 selenium 找到按钮并单击它并返回获取数据,但似乎即使我在代码中有按钮 ID,它也找不到它

"""
Search on Google and returns the list of PAA questions in SERP.
"""
def newSearch(browser,query):
    if lang== "en":
        browser.get("https://www.google.com?hl=en")
        WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe")))
        agree = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="L2AGLb"]/span/span'))) 
        agree.click()
        browser.switch_to_default_content()
        searchbox = browser.find_element_by_xpath("//input[@aria-label='Search']")
    else:
        browser.get("https://www.google.com?hl=es")
        searchbox = browser.find_element_by_xpath("//input[@aria-label='Buscar']")
    
    searchbox.send_keys(query)
    sleepBar(2)
    tabNTimes()
    if lang== "en":
        searchbtn = browser.find_elements_by_xpath("//input[@aria-label='Google Search']")
    else:
        searchbtn = browser.find_elements_by_xpath("//input[@aria-label='Buscar con Google']")
    try:
        searchbtn[-1].click()
    except:
        searchbtn[0].click()
    sleepBar(2)
    paa = browser.find_elements_by_xpath("//span/following-sibling::div[contains(@class,'match-mod-horizontal-padding')]")
    hideGBar()
    return paa
4

1 回答 1

0

尝试单击按钮本身的内部 div。同意弹出窗口的 HTML:

<button id="L2AGLb" class="tHlp8d" data-ved="0ahUKEwj89p7Swob1AhVBxhoKHS0gDxIQiZAHCCE">
    <div class="QS5gu sy4vM" role="none">
        Acepto
    </div>
</button>

您的选择器应如下所示:

(By.CSS_SELECTOR, "#L2AGLb > div")

这是一个完整的工作示例:

def test_google_...(self):
    driver = self.driver

    if self.LANGUAGE == "en":
        driver.get("https://www.google.com?hl=en")
    else:
        driver.get("https://www.google.com?hl=es")

    WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#L2AGLb > div"))
    )
    driver.find_element(By.CSS_SELECTOR, "#L2AGLb > div").click()

    WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, 'input[name="q"]'))
    )

    query = "your search query"
    driver.find_element(By.CSS_SELECTOR, 'input[name="q"]').send_keys(query)
    driver.find_element(By.CSS_SELECTOR, 'input[name="q"]').send_keys(Keys.RETURN)

    ...
于 2021-12-28T12:46:15.100 回答