通过className查找元素,然后单击JavaScript渲染网站中的元素使用Python Selenium

发布于 2025-02-14 01:12:11 字数 425 浏览 1 评论 0原文

我搜索了堆栈溢出并尝试了一些解决方案,但无法使其起作用。

这是我的代码:

from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe";
driver = webdriver.Chrome(PATH)

driver.get("https://coinmarketcap.com/");
driver.implicitly_wait(5)
tt = driver.find_element_by_class_name('zafg3t-1')
print(tt)

driver.quit()

类名称ZAFG3T-1是搜索区域类,我可以在浏览器中看到,但是Selenium找不到类。所以我无法点击。

我的错误是什么?

I searched stack overflow and tried some solutions but could not make it work.

Here is my code:

from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe";
driver = webdriver.Chrome(PATH)

driver.get("https://coinmarketcap.com/");
driver.implicitly_wait(5)
tt = driver.find_element_by_class_name('zafg3t-1')
print(tt)

driver.quit()

The class name zafg3t-1 is the search area class, I can see in browser, but selenium cant find the class. So I unable to click.

What is my mistake here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-21 01:12:11

类名称为 zafg3t-1 gawepq 等是动态生成的,并且在大量时间后会更改即使是下次您访问站点 farresh。


搜索 框中单击您需要诱导 webdriverwait 对于 emeltage_be_be_clickable() ,您可以使用以下任何一个 定位器策略

  • 使用 css_selector

      driver.get('https://coinmarketcap.com/')
    WebDriverWait(驱动程序,20).until(ec.element_to_be_clickable(((by.css_selector
    WebDriverWait(驱动程序,20).until(ec.element_to_be_clickable(((by.css_selector
     
  • 使用 xpath

      driver.get('https://coinmarketcap.com/')
    webdriverwait(驱动程序,20).until(ec.element_to_be_clickable(((by.xpath,“ // div [@class ='cmc-cookie-policy-banner__close'close'close'close']))。
    WebDriverWait(驱动程序,20).until(ec.element_to_be_clickable(((by.xpath,“ // div [text()='search']”))。
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     
  • 浏览器快照:

“

Classnames like zafg3t-1, gaWePq, etc are dynamic generated and would change after a considerable amount of time and even may be the next time you access the site afresh.


Solution

To click within the Search box you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://coinmarketcap.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.cmc-cookie-policy-banner__close"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-text='Use to trigger search']"))).click()
    
  • Using XPATH:

    driver.get('https://coinmarketcap.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='cmc-cookie-policy-banner__close']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Search']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

coinmarketcap

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文