【问题标题】:Getting Trouble in Dismissing Ads from Website using Selenium使用 Selenium 从网站中删除广告时遇到麻烦
【发布时间】:2021-09-17 18:54:42
【问题描述】:

我正在尝试进行网络自动化,我正在使用 selenium 库移动到一个页面以查找该页面的标题,但是当我尝试单击查找按钮时突然弹出广告并且它扰乱了流程并且它不会允许查找按钮单击它。让我知道如何关闭该广告,以便我可以转到下一页并获取该页面的磁贴。

这是我的代码:

#Using Selenium to move towards the next pages by clicking on button

#Libs Included 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

#Path to Chrome Driver 
path='chromedriver.exe'    
driver=webdriver.Chrome(path)
    
#Main_Url Page
main_url='https://www.zameen.com/'

#Getting the MainPage
driver.get(main_url)
print(driver.title)


#Selecting the Drop Down Menu First 
search=driver.find_element_by_class_name('eedc221b').click()


#How To Move to  Specific Area using  Finding Box To get All the List of Cities
list_of_cities=[]
Cities=driver.find_elements_by_class_name("d92d11c7")
#print(Cities)
for i in Cities:
    city=i.text
    list_of_cities.append(city)
#print("List of Cities are:  \n",list_of_cities)
    
#Reach towards the first Location by sending the citname to the combobox and then hit enter 
driver.find_element_by_css_selector("button[aria-label='"+Cities[0].text+"']").click()

time.sleep(3)

driver.find_element_by_css_selector("a[aria-label='Find button'][class='c3901770 _22dc5e0a']").click()

    
try:
    WebDriverWait(driver,10).until(EC.presence_of_element_located((By.TAG_NAME,"html")))
    print("Tilte of next Page is: {0}".format(driver.title))
    time.sleep(5)  
    driver.quit()


finally:
    driver.quit()

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    添加关闭按钮可以在下面css selector 的帮助下识别:

    # Path to Chrome Driver 
    path = 'chromedriver.exe'
    driver = webdriver.Chrome(path)
    wait = WebDriverWait(driver, 10)
    # Main_Url Page
    main_url = 'https://www.zameen.com/'
    driver.maximize_window()
    # Getting the MainPage
    driver.get(main_url)
    try:
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.close_cross_big"))).click()
    except:
        print("could not click")
        pass
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    然后您就可以继续编写其余代码了。

    【讨论】:

    • 请让我将这些行添加到我的脚本中
    • @AbdulRehman :检查上面更新的代码,我已经尝试使用您的代码复制相同的代码。
    猜你喜欢
    • 2023-03-31
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2021-03-24
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多