为什么selenium无法找到discus评论部分?

2024-10-02 02:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

你好

我以前在python上创建了一些selenium脚本,它们运行得很好,但不知道它为什么不能在网站上运行。 我试图点击一个元素,当“discus”被加载到那个网站时出现。 discus是网站上的评论部分,在网站加载后加载。在

因此,我使用了sleep方法,但它不起作用,所以我尝试使用python IDLE逐个执行代码行,这样我就可以运行locate code一次discus comment section完全可用,但仍然得到相同的错误。”Webdriver无法定位元素“

这是我的密码。在

import selenium from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait browser = webdriver.Chrome('E:\\ChromeDriver\\chromedriver_win32\\chromedriver.exe') browser.get('https://www.eloanus.com/customers-review') dicuss = browser.find_element_by_class_name('textarea') print(dicuss)

错误,我得到了。在

我附上了从那个网站上检查元素(类)的截图。在

Inspect element screen shot

我是编程新手,如果有任何帮助,我将不胜感激。在


Tags: fromimportbrowser脚本元素网站selenium错误
2条回答

有多个iframes,因此在需要交互之前必须切换到它。在

一旦你把司机的控制权切换到默认值,这是一个很好的做法。在

代码:

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 

driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://www.eloanus.com/customers-review")

wait = WebDriverWait(driver, 20)

visibilty_login = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Log In']")))

action = ActionChains(driver)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='disqusCommentsHolder']")))

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Disqus']")))

driver.execute_script("window.scrollTo(0, 100)") 

wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Join the discussion…']/following-sibling::div[@class='textarea']"))).send_keys("Hi")

driver.switch_to.default_content()  

希望这有帮助。在

要将字符序列发送到discus comment部分,您需要:

  • 诱导WebDriverWait使所需父帧可用,并切换到该帧
  • 诱导WebDriverWait以获得所需的嵌套帧,并切换到该帧
  • 诱导WebDriverWait使所需的元素可单击,您可以使用以下解决方案:
  • 代码块:

    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
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    browser.get('https://www.eloanus.com/customers-review') 
    WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='disq2disqusCommentsHolder']")))
    WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id,'dsq-app') and @title='Disqus']")))
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textarea'][contains(@aria-label,'Join the discussion')]"))).send_keys("Michael Berger")
    
  • 浏览器快照:

Disqus_comment_section

相关问题 更多 >

    热门问题