点击Instagram通知上的“Not now”,使用Selenium和Python询问

jucafojl  于 2023-06-28  发布在  Python
关注(0)|答案(5)|浏览(130)

我已经写了一个脚本,成功地使Instagram上的登录。当我应该去我的帐户,在家里,网站显示一个弹出,问你是否要通知。在这一点上,我尝试了很多解决方案,但我没有得到任何结果。我只是希望,当弹出窗口显示,脚本应该点击“不是现在”。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

ids = []

driver = webdriver.Chrome(executable_path = '/usr/local/bin/chromedriver')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")

usm = driver.find_element_by_name('username').send_keys("**")
pwd = driver.find_element_by_name('password').send_keys("**")
btnLog = driver.find_element_by_tag_name('form').submit()

acpt = driver.find_element_by_xpath("//*[contains(@class, 'aOOlW   HoLwm ')]")

在图像中,有突出显示的按钮行,我想单击:

u91tlkcl

u91tlkcl1#

请尝试以下代码:

from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()

PS:我已经用了10秒等待元素是可点击的,然后点击它。
希望能帮到你!

vddsk6oq

vddsk6oq2#

click()元素上的文本为Not nowInstagram 弹出通知上,您可以使用以下解决方案:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
driver.find_element_by_name('username').send_keys("Giacomo")
driver.find_element_by_name('password').send_keys("Maraglino")
driver.find_element_by_tag_name('form').submit()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Non ora')]"))).click()
mcvgt66p

mcvgt66p3#

只需在acpt中添加click,如下所示:

acpt = driver.find_element_by_xpath("//*[contains(@class, 'aOOlW   HoLwm ')]")
acpt.click()
628mspwn

628mspwn4#

出于某种原因,这个问题的解决方案对我来说并不完全有效。脚本没有点击“不是现在”,然后我被重定向到“主页”页面,只找到'激活通知'弹出窗口等着我。这是我提出的解决方案:
1.转到https://instagram.com/
1.等待页面加载。
1.使用完整的XPATH在按钮上找到“Not now”按钮。
1.点击它。
以下是代码(在识别到我的Instagram帐户后插入):

driver.get("https://instagram.com/")
    time.sleep(7)
    acpt = browser.find_element(by=By.XPATH, value='/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div/div[3]/button[2]')
    acpt.click()
hs1ihplo

hs1ihplo5#

导入以下selenium类来处理异常、发送密钥和创建时间延迟。并执行选项1-选项3来解决此问题

from selenium import webdriver
from selenium.common import NoSuchElementException, TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from time import sleep

# I used Chrome but you can used any browser
 
# ---- Optional - add options to keep the webpage open ----
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=options)

选项1)使用路径

try:
     notification_off = WebDriverWait(driver, 20).until(EC.presence_of_element_located(('xpath', '//*[@id="mount_0_0_LP"]/div/div/div[3]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]/button[2]')))
except TimeoutException:
    print("no such element found")
else:
    click_anywhere.click()

选项2)如果选项1不起作用。搜索按钮名称

sleep(20)
try:
     not_off = driver.find_element('name', 'Not Now')
except NoSuchElementException:
     print('No such element found')
else:
      not_off.click()

选项3)查找所有按钮并使用if语句查找所需内容

sleep(5)
try:
    notification_off = driver.find_elements('css selector', 'button')
except NoSuchElementException:
    print("notification element not found")
else:
    # dictionary comprehension
    not_off = [item for item in notification_off if item.text == "Not Now"]
    not_off[0].click()

相关问题