我想从https://www.techadvisor.co.uk/review/wearable-tech/"中抓取一些数据。我发现用Beautifulsoup在页面中循环是行不通的。这就是为什么我尝试用selenium打开它的原因。
我试过了:
browser = webdriver.Chrome()
browser.get("https://www.techadvisor.co.uk/review/wearable-tech/")
# button = browser.find_element_by_xpath('/html/body/div/div[3]/div[5]/button[2]')
# WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "html/body/div/div[3]/div[5]/button[2]"))).click()我总是收到NoSuchElementException
老实说,我发现Xpath真的很奇怪,但这是我从Google Chrome检查中得到的。
感谢每个解决方案建议或技巧:)
发布于 2020-08-14 06:27:47
要单击iframe.You中的Accept All按钮,需要先切换到iframe才能单击该按钮。
归纳WebDriverWait()并等待frame_to_be_available_and_switch_to_it(),然后使用下面的css选择器。
归纳WebDriverWait()并等待element_to_be_clickable(),然后使用下面的xpath选择器。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
browser.get("https://www.techadvisor.co.uk/review/wearable-tech/")
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='sp_message_iframe']")))
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept All']"))).click()发布于 2020-11-03 02:51:41
我知道这个问题很老,但我想提供我自己的解决方案!第一步是识别你实际查看的表单的"id“,然后你需要将焦点移到它上面!
driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="gdpr-consent-notice"]'))
cookies = driver.find_element_by_xpath('/html/body/app-root/app-theme/div/div/app-notice/app-theme/div/div/app-home/div/div[3]/div[2]/a[3]/span')
cookies.click()https://stackoverflow.com/questions/63403604
复制相似问题