我试着接受位于iframe中的cookie。但我所尝试的都不起作用。我愿意接受建议和正确的方法
下面是该网站的截图:
下面是我当前的代码:
from bs4 import BeautifulSoup
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
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
import datetime
import re
def scrap_ouestfrance_immo(driver):
URL = 'https://immobilier.lefigaro.fr/annonces/annonce-50413740.html'
driver.get(URL)
wait = WebDriverWait(driver, 6)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@style='border: 0px none; min-width: 100%; position: fixed; right: 0px; bottom: 0px; height: 100%; max-height: 100%; z-index: 2147483647']'")))
accept_all_button = driver.find_element(By.CSS_SELECTOR, 'button[class="sc-1epc5np-0 dnGUzk sc-f7uhhq-2 coEmEP button button--filled button__acceptAll"]')
accept_all_button.click()
driver.switch_to.parent_frame()
print('accepted cookies')
#Main
if __name__ == '__main__':
#opening browser
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox') # required when running as root user. otherwise you would get no sandbox errors.
driver = webdriver.Chrome(
# driver_path='/home/dev/chromedriver',
options=chrome_options,
service_args=[
'--verbose',
# '--log-path=/tmp/chromedriver.log'
]
)
scrap_ouestfrance_immo(driver)
#driver.quit()
我尝试使用Xpath,CSS选择器切换到iframe,但我无法做到。iframe没有特定的类或ID。我只看到切换到它的样式。但我可能错过了一些东西
谢谢大家
2条答案
按热度按时间fgw7neuy1#
用于查找和切换到iframe的XPath表达式不正确。请尝试以下XPath表达式:
代码应如下所示:
**XPath表达式说明:**基本上,这个XPath将在DOM结构中找到第二个iframe。
同时将定位“全部接受”按钮的代码更改为以下代码:
dgtucam12#
使用下面的
xpath
来标识iframe
元素,并使用下面的css选择器来标识cookie按钮。切换帧后,等待
element_to_be_clickable()