Need Help Python Selenium Script

baldeepo

Junior Member
Joined
Apr 25, 2013
Messages
119
Reaction score
28
I'm trying to automate script that go to my linktree link then click on "click here" and then click on pin and it is working perfectly.But when it click on pin, url open in new tab and my chrome driver close in few seconds automatically everytime it click on pin.And also i get unable to render error.Please help me to solve the problem.

I just want that chrome driver dont crash automatically.I just want to use chrome driver, not want to use firefox gecko driver.Please help me.


driver.maximize_window()
driver.get("https://linktr.ee/dogramilly982")
time.sleep(15)
title = driver.find_element(By.XPATH, "//*[contains(text(), 'Click Here')]")
driver.execute_script("arguments[0].click();", title)
time.sleep(8)

#after this code click on pin it open url in new tab and chrome driver crash automatically
pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
driver.execute_script("arguments[0].click();", pin)
time.sleep(8)
 
It looks like the issue you are experiencing may be caused by the fact that the "click here" link is opening a new tab in your web browser, which is causing the Chrome driver to close. One solution to this problem might be to use the "switch_to.window" method to switch the focus of the Chrome driver to the new tab that is opened.

Here's an example of how you might modify your code to do this:
Code:
driver.maximize_window()
driver.get("https://linktr.ee/dogramilly982")
time.sleep(15)
title = driver.find_element(By.XPATH, "//*[contains(text(), 'Click Here')]")
driver.execute_script("arguments[0].click();", title)
time.sleep(8)

# Switch to the new tab that was opened
driver.switch_to.window(driver.window_handles[1])

# After this code, click on the pin
pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
driver.execute_script("arguments[0].click();", pin)
time.sleep(8)
I hope this helps!
 
It looks like the issue you are experiencing may be caused by the fact that the "click here" link is opening a new tab in your web browser, which is causing the Chrome driver to close. One solution to this problem might be to use the "switch_to.window" method to switch the focus of the Chrome driver to the new tab that is opened.

Here's an example of how you might modify your code to do this:
Code:
driver.maximize_window()
driver.get("https://linktr.ee/dogramilly982")
time.sleep(15)
title = driver.find_element(By.XPATH, "//*[contains(text(), 'Click Here')]")
driver.execute_script("arguments[0].click();", title)
time.sleep(8)

# Switch to the new tab that was opened
driver.switch_to.window(driver.window_handles[1])

# After this code, click on the pin
pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
driver.execute_script("arguments[0].click();", pin)
time.sleep(8)
I hope this helps!
hi bro i used your modified code but it is giving me error IndexError: list index out of range .
 
Are you using the same version of your chrome and chromedriver?
try to put sleep time on less, that can cause chromedriver to crash
 
Are you using the same version of your chrome and chromedriver?
try to put sleep time on less, that can cause chromedriver to crash
yes i used time.sleep function also but still when it click on pin it open url in new tab then in less then milliscond browser auto close.i tried every idea found from other coding websites also.This error also come when i use driver switch handle selenium.common.exceptions.WebDriverException: Message: disconnected: unable to connect to renderer
 
First thing you can do is using of exception handling in your coding, that way you will not get unexpected crashes and you'll know what causes the issue:

pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
driver.execute_script("arguments[0].click();", pin)
The above code becomes:

Code:
try:
    pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
    driver.execute_script("arguments[0].click();", pin)
except Exception as e:
    print(e)
 
yes i used time.sleep function also but still when it click on pin it open url in new tab then in less then milliscond browser auto close.i tried every idea found from other coding websites also.This error also come when i use driver switch handle selenium.common.exceptions.WebDriverException: Message: disconnected: unable to connect to renderer
You didn't try enough. The reason why the browser window closes is because the script ended.

What you want to happen is to wait for the new tab that is opened after clicking the "pin" but the chromedriver is not set to allow pop-ups so the browser is closed. You must allow it through the arguments.

Here's the working code.

Python:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
import time

options = webdriver.ChromeOptions()
options.add_argument("--disable-popup-blocking")
driver = webdriver.Chrome(options)
driver.maximize_window()
driver.get("https://linktr.ee/dogramilly982")
time.sleep(8)
title = driver.find_element(By.XPATH, "//*[contains(text(), 'Click Here')]")
driver.execute_script("arguments[0].click();", title)
time.sleep(8)
# After this code, click on the pin
pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
driver.execute_script("arguments[0].click();", pin)
time.sleep(8)
# now uncomment the below code and do whatever you want to do with the new tab that is opened after clicking the "pin"
# driver.switch_to.window(driver.window_handles[1])
#time.sleep(4)
 
First thing you can do is using of exception handling in your coding, that way you will not get unexpected crashes and you'll know what causes the issue:


The above code becomes:

Code:
try:
    pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
    driver.execute_script("arguments[0].click();", pin)
except Exception as e:
    print(e)
This code is skipping the error.But when after clicking button url open in new tab then chromedriver again closing and repeating process again and again url open in new tab and again close in millisecond.
 
closing and repeating process again and again url open
So you have wrapped the process in some kind of loop?

If the exception is passing the error, you need to find what is the thrown error at the console view, from the print(e) command, then debug based on that.
 
You didn't try enough. The reason why the browser window closes is because the script ended.

What you want to happen is to wait for the new tab that is opened after clicking the "pin" but the chromedriver is not set to allow pop-ups so the browser is closed. You must allow it through the arguments.

Here's the working code.

Python:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
import time

options = webdriver.ChromeOptions()
options.add_argument("--disable-popup-blocking")
driver = webdriver.Chrome(options)
driver.maximize_window()
driver.get("https://linktr.ee/dogramilly982")
time.sleep(8)
title = driver.find_element(By.XPATH, "//*[contains(text(), 'Click Here')]")
driver.execute_script("arguments[0].click();", title)
time.sleep(8)
# After this code, click on the pin
pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
driver.execute_script("arguments[0].click();", pin)
time.sleep(8)
# now uncomment the below code and do whatever you want to do with the new tab that is opened after clicking the "pin"
# driver.switch_to.window(driver.window_handles[1])
#time.sleep(4)
This code is working perfectly when i normally use without chrome profile.But i m using chrome profile with all extentions loaded then still same thing is happening.This is upper code i m using

chrome_options = Options()
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_experimental_option("excludeSwitches", ["disable-popup-blocking"])
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument(r"--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\")
chrome_options.add_argument('--profile-directory=Profile 2')
capabilities = DesiredCapabilities.CHROME.copy()

chromedriver = r"C:\\Program Files (x86)\\chromedriver.exe"

driver = webdriver.Chrome(executable_path=chromedriver, chrome_options = chrome_options, desired_capabilities=capabilities)
 
So you have wrapped the process in some kind of loop?

If the exception is passing the error, you need to find what is the thrown error at the console view, from the print(e) command, then debug based on that.
yes i m using by this
for i in range(2000):
 
This code is working perfectly when i normally use without chrome profile.But i m using chrome profile with all extentions loaded then still same thing is happening.This is upper code i m using

That is not what you described in your OP. The solution with 100% working code is provided for what you initially asked for.

I just want that chrome driver dont crash automatically.I just want to use chrome driver, not want to use firefox gecko driver.Please help me.
 
No one has recommended WebDriverWait before. You should try putting it in a try catch expression along with WebDriverWait and EC(expected conditions). You want to make sure the element exists in the DOM before you try to find it/click it.


Edit: replace all time.sleep() with WebDriverWait. You can never be absolutely sure that the element will be available or clickable within the given wait period in time.sleep. WebDriverWait with a high enough wait period like 2 mins should give a good idea if it's due to stale/non-existent elements or some other errors(our try except block would catch it)
 
instead of trying to click the button which will take to a new page ! try to get the href link from it then user driver.get() the new link which will open the page in the same tab
 
It looks like you are encountering an issue where the Chrome driver is closing automatically after clicking the "pin" element.

There are a few things you can try to fix this issue:

  1. Make sure you are using the latest version of the Chrome driver. You can check for updates on the ChromeDriver website (https://chromedriver.chromium.org/downloads).
  2. Try adding a longer sleep time after clicking the "pin" element to give the browser more time to load the new tab.
  3. Make sure you have closed all other Chrome windows before running the script. Sometimes, having multiple Chrome windows open can cause issues with the Chrome driver.
  4. You can also try adding some error handling to your script to catch any exceptions that may be causing the Chrome driver to close.

For example:
try:
pin = driver.find_element(By.XPATH, "//span[@data-pin-log='embed_pin']")
driver.execute_script("arguments[0].click();", pin)
except Exception as e:
print(e)
This will print out any exceptions that may have occurred, which can help you identify the cause of the issue.
 
When a new tab opens the driver sometimes loses track of the original window context. You might need to switch to the new tab handle after the click. Stackoverflow has a few snippets for that exact problem if you search for selenium switch to new tab.
 
@baldeepo the issue is definitely your user data dir. if you have your normal chrome browser open while running the script, selenium will crash or disconnect from the renderer because the profile is locked by the system. i had this exact same issue last year running some pinterest bots...

you should copy the 'Profile 2' folder to a completely separate directory, like C:\selenium_profiles\profile2, and point your script there instead of your live user data path. also tbh loading a profile with tons of extensions for a 2000 loop run is going to leak memory and crash your driver anyway. try to only load the absolute necessary extensions using add_extension instead of loading the whole user profile.
 
yeah @Rankings Daily is right about the profile lock. if you have chrome open normally while running this it will crash every time because of the user data dir lock.

but honestly if you are running this 2000 times in a loop, you really shouldn't be using your main chrome profile anyway. those extensions will bloat the memory and crash the driver after like 50 runs. i had this exact issue with a pinterest bot last year.

what worked for me was creating a completely fresh profile just for the script, and then adding only the crx files of the extensions i actually needed using chrome_options.add_extension(). also check your task manager to make sure there arent any ghost chrome.exe processes running in the background locking the profile before you start the script.
 
Honestly @Heixenberg has the best workaround here. If you are running this 2000 times, opening new tabs and switching handles is going to eat up your RAM and crash the renderer anyway, especially with a heavy chrome profile loaded. i had a similar issue with a mass pinner last year, selenium just hates multi-tabs when you are running heavy profiles.

Just grab the href attribute from the element and use driver.get() to load it in the same tab. It is way faster and bypasses the whole window switching mess.

If you absolutely have to click it and get that index out of range error, it is because the script tries to switch to the second tab before the browser actually opens it. You need a quick loop to wait for the handle to exist first.

Something like:
Code:
while len(driver.window_handles) < 2:
    time.sleep(0.5)
driver.switch_to.window(driver.window_handles[1])

But yeah, grabbing the href is much cleaner for loop runs imo.
 
This happens because Selenium loses the handle of the main window due to opening of the new tab and upon the completion of the script. It would be better to use `driver.window handles` and continue with a time.sleep wait after the click. Additionally, ensure that Chrome is not operating in headless mode, and do not use driver.quit till all the tabs complete
 
Back
Top