InstaPy icon indicating copy to clipboard operation
InstaPy copied to clipboard

Accepted IG cookies by default... problem

Open kharazian opened this issue 3 years ago • 7 comments

Expected Behavior

IG should find and click on Accept All cookie.

Current Behavior

It is xPath problem and IG could not find Accept All bottom.

Possible Solution (optional)

the xPath should change from xpath["accept_igcookie_dialogue"] = {"accept_button": "//button[text()='Accept']"} to xpath["accept_igcookie_dialogue"] = {"accept_button": "//button[text()='Accept All']"}

kharazian avatar Oct 26 '21 05:10 kharazian

Hi @kharazian

I tried your solution. It also seems to get stuck at the 'Sign up to see photos and videos from your friends' Instagram app prompt. It cannot find the login button.

INFO [2021-10-30 22:17:30] [*******] Timed out with failure while explicitly waiting until visibility of element located!

InstaPy version 0.6.14

Traceback (most recent call last): File "C:\Users\PC\Desktop\instagram_py\bot.py", line 4, in session.login() File "C:\Users\PC\Desktop\instagram_py\env\lib\site-packages\instapy\instapy.py", line 430, in login if not login_user( File "C:\Users\PC\Desktop\instagram_py\env\lib\site-packages\instapy\login_util.py", line 373, in login_user input_username = browser.find_element_by_xpath(input_username_XP) File "C:\Users\PC\Desktop\instagram_py\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 514, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "C:\Users\PC\Desktop\instagram_py\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1238, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Users\PC\Desktop\instagram_py\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute self.error_handler.check_response(response) File "C:\Users\PC\Desktop\instagram_py\env\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@name='username'] Stacktrace: WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:181:5 NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:393:5 element.find/</<@chrome://remote/content/marionette/element.js:305:16

theclassicsponge avatar Oct 30 '21 22:10 theclassicsponge

I found a solution for both of your issues (quick & dirty fix!)

1) To get pass the cookie dialog make sure xpath search corresponds to the actual accept button in this case "Accept All" instead of "Accept".

To do this, go to the xpath_compile.py file and change the last line from

xpath["accept_igcookie_dialogue"] = {"accept_button": "//button[text()='Accept']"}

to

xpath["accept_igcookie_dialogue"] = {"accept_button": "//button[text()='Accept All']"}

2) To get pass the "Sign up to see photos and videos from your friends" page I injected some lines to click on the "Log In" button before moving on entering the credentials.

I did this right after the cookie dialog. Thus, change the "accept_igcookie_dialogue" def in the login_util.py file from

def accept_igcookie_dialogue(browser, logger):
    """Presses 'Accept' button on IG cookie dialogue"""

    offer_elem_loc = read_xpath(accept_igcookie_dialogue.__name__, "accept_button")

    offer_loaded = explicit_wait(
        browser, "VOEL", [offer_elem_loc, "XPath"], logger, 4, False
    )

    if offer_loaded:
        logger.info("- Accepted IG cookies by default...")
        accept_elem = browser.find_element_by_xpath(offer_elem_loc)
        click_element(browser, accept_elem)

to


def accept_igcookie_dialogue(browser, logger):
    """Presses 'Accept' button on IG cookie dialogue"""

    offer_elem_loc = read_xpath(accept_igcookie_dialogue.__name__, "accept_button")

    offer_loaded = explicit_wait(
        browser, "VOEL", [offer_elem_loc, "XPath"], logger, 4, False
    )

    if offer_loaded:
        logger.info("- Accepted IG cookies by default...")
        accept_elem = browser.find_element_by_xpath(offer_elem_loc)
        click_element(browser, accept_elem)

    """New Login Dialog Workaround"""

    login_elem_loc = read_xpath("mobile_click_login", "login_button")

    login_loaded = explicit_wait(
        browser, "VOEL", [login_elem_loc, "XPath"], logger, 4, False
    )

    if login_loaded:
        logger.info("Login loaded...")
        login_elem = browser.find_element_by_xpath(login_elem_loc)
        click_element(browser, login_elem)

Correspondingly we need to add the corresponding xpath in the xpath_compile.py file. Therefore, add the following line to it:

xpath["mobile_click_login"] = {"login_button": "//button[text()='Log In']"}

Let me know if this works for you!

Cheers Andreas

andrsschultz avatar Oct 31 '21 16:10 andrsschultz

Hi @andrsschultz

That works!

Thanks

theclassicsponge avatar Nov 02 '21 23:11 theclassicsponge

Nice fix ! Thanks.

benjamrio avatar Feb 11 '22 18:02 benjamrio

Think its more time for a new update via pip

Tr1pke avatar Apr 27 '22 14:04 Tr1pke

Hi,

Thanks for the fix.

However, I cannot login into Instagram after correcting both issues.

Traceback (most recent call last): File "quickstart.py", line 19, in with smart_run(bot): File "/usr/lib/python3.8/contextlib.py", line 113, in enter return next(self.gen) File "/usr/local/lib/python3.8/dist-packages/instapy/util.py", line 1983, in smart_run session.login() File "/usr/local/lib/python3.8/dist-packages/instapy/instapy.py", line 433, in login if not login_user( File "/usr/local/lib/python3.8/dist-packages/instapy/login_util.py", line 297, in login_user accept_igcookie_dialogue(browser, logger) File "/usr/local/lib/python3.8/dist-packages/instapy/login_util.py", line 718, in accept_igcookie_dialogue login_elem = browser.find_element(login_elem_loc) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 855, in find_element return self.execute(Command.FIND_ELEMENT, { File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 428, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidArgumentException: Message: unknown variant //button[text()='Log In'], expected one of css selector, link text, partial link text, tag name, xpath at line 1 column 37

jeromecoffin avatar Sep 10 '22 13:09 jeromecoffin

@andrsschultz I am getting:

KeyError: 'mobile_click_login'

TommyTrill89 avatar May 29 '23 19:05 TommyTrill89