Python Selenium:找到并点击webdriver >工具提示是可见的,没有“id”或“名称”

我的问题是:我试图登录Wikifolio。com与Python和硒使用webdriver。

到目前为止,登录工作正常(对selenium的实际功能有非常基本的了解)。

我还设法点击了即将出现的免责声明按钮。

然后是一个“交易”按钮列表,每个按钮都连接到不同的资产。

当我点击“检查”的交易按钮,我得到以下xpath从网站:

//*[@id="root-wf-trading"]/div/div/table/tbody/tr[3]/td/table/tbody/tr[6]/td/div/a[1]

或者完整的xpath

/html/body/div[3]/main/div[3]/div[2]/div[1]/div/div/div[1]/div[2]/div/div/div[1]/div/div/table/tbody/tr[3]/td/table/tbody/tr[6]/td/div/a[1]

相应的HTML是

<a href="#add_virtual_order" rel="US88554D2053" data-amount="999" data-issuer="1900" data-description="3 D SYS CORP.     DL-,001" data-security-type="620" class="add_virtual_order js-trade-button inline c-btn btn-trade-xsm u-mr-0 u-fs-s" data-main-editable="true" data-toggle="tooltip" data-placement="top" data-original-title="<div class='wikifolio-tooltip-text'>Neuen Trade platzieren</div>" aria-describedby="tooltip436117">Trade</a>

到目前为止,我使用的Python代码如下:

from selenium import webdriver
username = "*********"
password = "*********"
url = "https://www.wikifolio.com/dynamic/de/de/login/login?ReturnUrl=/de/de/home&_=1632037782306"
driver = webdriver.Chrome(executable_path=r'\Users\Benjamin\Downloads\chromedriver_win32\chromedriver.exe')
driver.get(url)
driver.find_element_by_name("Username").send_keys(username)
driver.find_element_by_name("Password").send_keys(password)
driver.find_element_by_css_selector("button").click()
driver.get("https://www.wikifolio.com/de/de/meine-wikifolios/trade/wf00wiking")
driver.find_element_by_css_selector(".c-disclaimer .js-disclaimer__abort, .c-disclaimer .js-disclaimer__change").click()
driver.find_element_by_xpath('/html/body/div[3]/main/div[3]/div[2]/div[1]/div/div/div[1]/div[2]/div/div/div[1]/div/div/table/tbody/tr[3]/td/table/tbody/tr[6]/td/div/a[1]').click()

当我尝试运行这段代码出现以下情况:

NoSuchElementException                    Traceback (most recent call last)
<ipython-input-47-2808785f4fd0> in <module>
     18 
     19 
---> 20 driver.find_element_by_css_selector(".c-disclaimer .js-disclaimer__abort, .c-disclaimer .js-disclaimer__change").click()
     21 
     22 
~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_css_selector(self, css_selector)
    596             element = driver.find_element_by_css_selector('#foo')
    597         """
--> 598         return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
    599 
    600     def find_elements_by_css_selector(self, css_selector):
~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    976         return self.execute(Command.FIND_ELEMENT, {
    977             'using': by,
--> 978             'value': value})['value']
    979 
    980     def find_elements(self, by=By.ID, value=None):
~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))
~\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".c-disclaimer .js-disclaimer__abort, .c-disclaimer .js-disclaimer__change"}
  (Session info: chrome=93.0.4577.82)

我能做什么?我如何找到及打开“交易”按钮?

非常感谢,并祝福本杰明

###你的定位器肯定不好,这就是为什么你得到NoSuchElementException.
。< br / >

为了给你正确的定位器我需要用有效的身份进入。< br / >

基于您提供的元素HTML而不是

driver.find_element_by_xpath('/html/body/div[3]/main/div[3]/div[2]/div[1]/div/div/div[1]/div[2]/div/div/div[1]/div/div/table/tbody/tr[3]/td/table/tbody/tr[6]/td/div/a[1]').click()

试试这个:

driver.find_element_by_xpath("//a[@data-toggle='tooltip' and(contains(@href,'add_virtual_order')) and(contains(@class,'add_virtual_order'))]").click()

此外,你必须在访问元素之前添加等待/延迟,就像这样:

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
username = "*********"
password = "*********"
url = "https://www.wikifolio.com/dynamic/de/de/login/login?ReturnUrl=/de/de/home&_=1632037782306"
driver = webdriver.Chrome(executable_path=r'\Users\Benjamin\Downloads\chromedriver_win32\chromedriver.exe')
wait = WebDriverWait(driver, 20)
driver.get(url)
driver.find_element_by_name("Username").send_keys(username)
driver.find_element_by_name("Password").send_keys(password)
driver.find_element_by_css_selector("button").click()
driver.get("https://www.wikifolio.com/de/de/meine-wikifolios/trade/wf00wiking")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".c-button--ghost"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[@data-toggle='tooltip' and(contains(@href,'add_virtual_order')) and(contains(@class,'add_virtual_order'))]"))).click()
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码