通过className查找元素,然后单击JavaScript渲染网站中的元素使用Python Selenium
我搜索了堆栈溢出并尝试了一些解决方案,但无法使其起作用。
这是我的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类名称为
zafg3t-1
,gawepq
等是动态生成的,并且在大量时间后会更改即使是下次您访问站点 farresh。)
在 搜索 框中单击您需要诱导 webdriverwait 对于 emeltage_be_be_clickable() ,您可以使用以下任何一个 定位器策略 :
使用 css_selector :
使用 xpath :
注意:您必须添加以下导入:
浏览器快照:
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:
Using XPATH:
Note: You have to add the following imports :
Browser Snapshot: