From 7e37ea86d0b305901161c0f65d8c3052d8edaba0 Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Wed, 4 Jun 2025 16:21:07 +0200 Subject: [PATCH] better popup removal method --- .../InstagramRepost/InstagramRepostJob.php | 61 ++++++++----------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php b/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php index 9757f78..76afda6 100644 --- a/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php +++ b/app/Browser/Jobs/InstagramRepost/InstagramRepostJob.php @@ -304,7 +304,7 @@ class InstagramRepostJob extends BrowserJob implements ShouldBeUniqueUntilProces sleep(2); $browser->attach('input[type="file"]._ac69', $this->downloadFolder . $reel->reel_id . ".mp4"); - sleep(5); // TODO : Wait for the file to be uploaded + sleep(10); // TODO : Wait for the file to be uploaded $this->removePopups($browser); sleep(2); @@ -340,13 +340,13 @@ class InstagramRepostJob extends BrowserJob implements ShouldBeUniqueUntilProces try { $browser->waitForText("Your post was shared", 60, true); $closeButton = $browser->driver->findElement(WebDriverBy::xpath('//div[./div/*[local-name() = "svg"][@aria-label="Close"]]')); - $closeButton->click(); + } catch (\Exception $e) { - // Do nothing + Log::error("Failed to repost reel: {$reel->reel_id} - " . $e->getMessage()); + $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); + AllNotification::send(new JobDebugNotification($this->jobId, "Failed to repost reel: {$reel->reel_id} - " . $e->getMessage())); + $closeButton->click(); } - Log::error("Failed to repost reel: {$reel->reel_id} - " . $e->getMessage()); - $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); - AllNotification::send(new JobDebugNotification($this->jobId, "Failed to repost reel: {$reel->reel_id} - " . $e->getMessage())); return false; } @@ -406,35 +406,28 @@ class InstagramRepostJob extends BrowserJob implements ShouldBeUniqueUntilProces protected function removePopups(Browser $browser) { - try { - $cookiesAllowButton = $browser->driver->findElement(WebDriverBy::xpath('//button[contains(text(), "Allow all cookies")]')); - $cookiesAllowButton->click(); - sleep(2); - } catch (\Exception $e) { - // No cookie popup found, continue - } + $popupsTypes = [ + ['//button[contains(text(), "Allow all cookies")]'], // Allow all cookies + ['//button[contains(text(), "Not Now")]', ["Popup Not Now clicked"]], // Not now + ['//button[contains(text(), "OK")]', ["Popup Ok clicked"]], // OK + ]; - try { - $notNowButton = $browser->driver->findElement(WebDriverBy::xpath('//button[contains(text(), "Not Now")]')); - $notNowButton->click(); - $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); - AllNotification::send(new JobDebugNotification($this->jobId, "Popup Not Now clicked")); - sleep(2); - } catch (\Exception $e) { - // No "Not Now" popup found, continue + foreach ($popupsTypes as $popup) { + try { + $button = $browser->driver->findElement(WebDriverBy::xpath($popup[0])); + if ($button === null) { + continue; // No button found, continue to the next popup + } + if (isset($popup[1])) { + $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); + AllNotification::send(new JobDebugNotification($this->jobId, $popup[1][0])); + } + $button->click(); + sleep(2); + return; // Exit after clicking the first popup found + } catch (\Exception $e) { + // Porbably no popup found, continue + } } - - try { - $okButton = $browser->driver->findElement(WebDriverBy::xpath('//button[contains(text(), "OK")]')); - $okButton->click(); - $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); - AllNotification::send(new JobDebugNotification($this->jobId, "Popup Ok clicked")); - sleep(2); - } catch (\Exception $e) { - // No "Ok" popup found, continue - } - - // $browser->script('document.querySelector("div.app-modal")[0].remove();'); - // $browser->driver->executeScript('document.querySelector("div.app-modal")[0].remove();'); } }