Spaces:
Running
Running
Create remove_element.py
Browse files- remove_element.py +51 -0
remove_element.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from selenium import webdriver
|
2 |
+
from selenium.webdriver.chrome.options import Options
|
3 |
+
from selenium.webdriver.common.by import By
|
4 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
5 |
+
from selenium.webdriver.support import expected_conditions as EC
|
6 |
+
from selenium.common.exceptions import TimeoutException
|
7 |
+
import time
|
8 |
+
|
9 |
+
def remove_element_from_facebook(url):
|
10 |
+
# Set up Chrome options
|
11 |
+
chrome_options = Options()
|
12 |
+
chrome_options.add_argument("--headless")
|
13 |
+
chrome_options.add_argument("--no-sandbox")
|
14 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
15 |
+
chrome_options.add_argument("--disable-gpu")
|
16 |
+
chrome_options.add_argument("--window-size=1920,1080")
|
17 |
+
chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
18 |
+
|
19 |
+
# Initialize the driver
|
20 |
+
driver = webdriver.Chrome(options=chrome_options)
|
21 |
+
|
22 |
+
try:
|
23 |
+
# Navigate to the URL
|
24 |
+
driver.get(url)
|
25 |
+
|
26 |
+
# Wait for the page to load
|
27 |
+
WebDriverWait(driver, 10).until(
|
28 |
+
EC.presence_of_element_located((By.TAG_NAME, "body"))
|
29 |
+
)
|
30 |
+
|
31 |
+
# Locate the element by its class name and remove it
|
32 |
+
try:
|
33 |
+
element_to_remove = driver.find_element(By.CLASS_NAME, "x9f619")
|
34 |
+
driver.execute_script("""
|
35 |
+
var element = arguments[0];
|
36 |
+
element.parentNode.removeChild(element);
|
37 |
+
""", element_to_remove)
|
38 |
+
print("Element removed successfully.")
|
39 |
+
except Exception as e:
|
40 |
+
print(f"Failed to remove element: {e}")
|
41 |
+
|
42 |
+
# Optionally, save the modified page source for verification
|
43 |
+
with open("/Users/a2014/urld/modified_page.html", "w", encoding="utf-8") as f:
|
44 |
+
f.write(driver.page_source)
|
45 |
+
|
46 |
+
finally:
|
47 |
+
driver.quit()
|
48 |
+
|
49 |
+
# Example usage
|
50 |
+
user_input_url = input("Enter the Facebook page URL: ")
|
51 |
+
remove_element_from_facebook(user_input_url)
|