Need Help Python Selenium Script

@baldeepo IndexError means there is no second tab at that moment, so either popup blocked, tab not opened yet, or chrome crashed before selenium can see the handle. Dont switch with window_handles[1] directly, save old handles first and wait for new one.

Code:
old_tabs = driver.window_handles

driver.execute_script("arguments[0].click();", pin)

WebDriverWait(driver, 15).until(lambda d: len(d.window_handles) > len(old_tabs))

new_tab = list(set(driver.window_handles) - set(old_tabs))[0]
driver.switch_to.window(new_tab)

But if it works without chrome profile and crashes only with profile/extensions, then the tab code is not the real problem imo. Your profile is most likely locked or one extension is killing renderer. Make a fresh selenium-only chrome profile and load only needed extension, dont use your daily chrome profile. Also close all chrome.exe from task manager before running, even one background process can mess it up.
 
that unable to connect to renderer error usually means the actual chrome browser crashed in the background before selenium could even grab the new window handle. usually happens on resource heavy pages or when pinterest tries to load heavy tracking scripts in the new tab.

try adding these arguments to your chrome options before starting the driver:

Code:
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')

if you are already doing that, check if your chrome version auto-updated in the background. chrome updates silently sometimes and then suddenly your chromedriver mismatch starts throwing weird renderer errors.
 
Your browser may not actually be “crashing”, sometimes selenium just closes it because your script ends right after the click. First test with detach + input at end, so you can see if chrome stays open.

Code:
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
options.add_argument("--user-data-dir=C:/selenium-profile")

driver = webdriver.Chrome(options=options)

old_tabs = driver.window_handles
driver.execute_script("arguments[0].click();", pin)

WebDriverWait(driver, 20).until(lambda d: len(d.window_handles) > len(old_tabs))
new_tab = [x for x in driver.window_handles if x not in old_tabs][0]
driver.switch_to.window(new_tab)

input("press enter to close")

If it still throws “unable to connect to renderer” before the input line, then yeah chrome itself is dying. In that case dont use your normal chrome profile, make a clean selenium profile like above and make sure chrome + chromedriver are same major version. Had same issue with Pinterest embeds before, one extension in my regular profile was killing the renderer instantly.
 
@baldeepo honestly if you are clicking a pinterest embed pin, those widgets are super heavy and have weird anti-bot scripts that crash automated chrome instances. instead of trying to click it and dealing with the tab crash, why not just grab the redirect link from the parent element and navigate directly? saves you the headache of handling new tabs.

try something like this to bypass the click:

Code:
pin_element = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']/ancestor::a")
pin_url = pin_element.get_attribute("href")
driver.get(pin_url)

if the href isn't on the ancestor, just inspect the HTML and find where the actual pinterest link is stored. navigating directly is way cleaner than letting pinterests JS break your driver...
 
@PPCPirate has the right idea, grabbing the href and doing driver.get is way less painful than fighting the new tab. That renderer disconnect is happening before selenium even sees the second handle, thats why you get the IndexError...chrome is basically already dead by the time switch_to runs, so the window_handles[1] fix cant help you.

If you still wanna click it for some reason, just strip the target=_blank first so it stays in the same tab and you never spawn the tab that crashes:

Code:
pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']/ancestor::a")
driver.execute_script("arguments[0].removeAttribute('target');", pin)
driver.execute_script("arguments[0].click();", pin)

One thing nobody mentioned, that embed_pin span is usually sitting inside a pinterest iframe. If your xpath sometimes cant even find it, you likely need driver.switch_to.frame on the pinterest widget first, then grab the element. But imo just pull the href and navigate, skip the whole widget circus.

And yeah do the chrome vs chromedriver major version check like @srdjan87 said, the "unable to connect to renderer" thing goes real weird on a mismatch.
 
honestly if you are doing any automation on pinterest or linktree with basic selenium you are gonna get flagged instantly. that renderer crash is usually just pinterests anti bot script killing the thread when it detects the automated click. @PPCPirate is right about grabbing the href, but if you absolutely need the click to register the referral traffic, try using undetected-chromedriver instead of standard selenium.

it bypasses most of those instant driver crashes. install it and run it like this:

Code:
import undetected_chromedriver as uc
driver = uc.Chrome()
driver.get("https://linktr.ee/dogramilly982")

also make sure you arent running headless because pinterest hates that. if you have to run headless use pyvirtualdisplay instead of the chrome headless flag, otherwise the renderer will keep disconnecting on you.
 
Try setting page load strategy to none/eager before blaming the click code. Pinterest tab can hang while loading all their JS, then selenium waits for render and you get that renderer disconnect.

Code:
from selenium.webdriver.chrome.options import Options

options = Options()
options.page_load_strategy = "none"
options.add_argument("--disable-extensions")
options.add_argument("--disable-features=RendererCodeIntegrity")
options.add_argument("--user-data-dir=C:/selenium-clean-profile")

driver = webdriver.Chrome(options=options)

Also don’t use your normal Chrome profile, that causes random locked profile/crash stuff. Make a clean folder like above. After clicking, wait for handles like @XSMMDoctor showed, but if Chrome dies before new handle appears then it’s not a tab switching problem anymore.

And click the parent a tag, not the span. That span is just inside the widget, sometimes JS click on it behaves different than real anchor click.
 
yeah that renderer crash is almost always anti-bot detection killing the tab process. pinterest is super aggressive with automation detection these days. if you really want to stick with selenium instead of just grabbing the href, you need to hide the webdriver flags or chrome will just dump the renderer thread. try adding this to your options:

Code:
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

tbh switching to playwright would save you a ton of headache here... it handles multiple tabs and contexts way cleaner than selenium without constantly crashing.
 
Back
Top