简体   繁体   English

如何使用 selenium 在 python 中执行 JavaScript 代码?

[英]How do I execute a JavaScript code in python using selenium?

I try to extract some info through selenium using these:我尝试使用这些通过硒提取一些信息:

from selenium import webdriver  # $ pip install selenium
from selenium.webdriver.chrome.options import Options

path = 'C:/Users/Жираслан/Downloads/chromedriver_win32 (1)/chromedriver.exe'
wd = webdriver.Chrome(executable_path=path)
result = wd.find_element_by_class_name('wrapper__top_3UDaE')

when the code in the web-page's elements are:当网页元素中的代码是:

<div class="wrapper__top_3UDaE">
    <h3>Лидерборд</h3>
    <span class="wrapper__description_nfMmy">...</span>
</div>

and after之后

result = wd.find_element_by_class_name('wrapper__top_3UDaE')
result

I get the next error:我收到下一个错误:

Message: no such element: Unable to locate element: {"method":"css selector","selector":".wrapper__top_3UDaE"}
  (Session info: chrome=86.0.4240.75)

Please, give me a hint on how to implement this correctly.请给我一个关于如何正确实现它的提示。 Thanks.谢谢。

In your example, you give us the HTML code for a div在您的示例中,您为我们提供了divHTML代码

<div class="wrapper__top_3UDaE">
    <h3>Лидерборд</h3>
    <span class="wrapper__description_nfMmy">...</span>
</div>

To get your element, you will need the following imports要获取您的元素,您将需要以下导入

from selenium import webdriver
from selenium.webdriver.common.by import By

Then, you can use this xpath然后,您可以使用此xpath

//div[@class='wrapper__top_3UDaE']

and you should be able to get your element你应该能够得到你的元素

wd = webdriver.Chrome(executable_path=path)
result = wd.find_element(By.XPATH, "//div[@class='wrapper__top_3UDaE']")

If your div is a button, you can click on your element如果你的div是一个按钮,你可以点击你的元素

wd = webdriver.Chrome(executable_path=path)
result = wd.find_element(By.XPATH, "//div[@class='wrapper__top_3UDaE']")
result.click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python Selenium中执行JavaScript代码,然后在我的Python代码中调用它? - How do i execute JavaScript code in Python Selenium and then call it back in my Python code? 在 Python 中使用 Selenium 执行 Javascript - Execute Javascript using Selenium in Python 如何使用 Python+Selenium 从 execute_script 获取 JS 控制台响应代码? - How do I get JS Console response code from execute_script with Python+Selenium? 我如何执行javascript以使用python和selenium更改日期 - How can I execute javascript to change a date with python and selenium Javascript void(0)单击或使用Selenium Python执行 - Javascript void(0) click or execute using selenium python 如何使用Selenium在python中执行Javascript函数 - How to execute a Javascript function in python with selenium Selenium Python绑定:如何在元素上执行JavaScript? - Selenium Python bindings: how to execute JavaScript on an element? 如何使用python在URL中捕获JavaScript代码注入? - How do I catch javascript code injection in URL using python? 如何在 JavaScript 中按顺序执行这两个不同的代码块? - How do I execute these 2 different blocks of code in a sequence in JavaScript? Python Selenium如何在带参数的JavaScript元素上执行execute_script - Python selenium how to execute execute_script on a JavaScript element with arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM