0

以下のコードの①ではエラーもなく押下できますが②だと③のエラーが出力されてしまいます。
原因を教えていただきたいです。関係無いかもしれませんがpip listの結果もコードの下に貼りました。
① driver.find_element(By.LINK_TEXT, "ログイン")
② driver.find_element(By.LINK_TEXT, "FX取引スタート")
③ Message: no such element: Unable to locate element: {"method":"link text","selector":"FX取引スタート"}
(Session info: chrome=103.0.5060.114)

↓ここからpythonコード

import time
import chromedriver_binary
from selenium import webdriver
from selenium.webdriver.common.by import By
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#ドライバーを自動でインストールしてくれる
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) 

#最大の読み込み時間を設定 今回は最大30秒待機できるようにする
wait = WebDriverWait(driver=driver, timeout=30)

def openSBI():
    try:
        # 開くURL
        url = "https://www.sbifxt.co.jp/login.html"
        # utlを開く
        driver.get(url)
        # 要素が全て検出できるまで待機する
        wait.until(EC.presence_of_all_elements_located)
        # //<input type="text" name="ID" id="loginid" class="fz12">
        name = driver.find_element(By.ID,"loginid")
        name.send_keys("1234567890")
        # //<input type="password" name="PASS" id="password" class="fz12">
        name = driver.find_element(By.ID,"password")
        name.send_keys("Password1234")
        # <a href="javascript:postLoginUrl(MPAGE_URL);" onclick="$(this).click(function(e){ return false });">ログイン</a>
        button = driver.find_element(By.LINK_TEXT, "ログイン")
        button.click()
        # 要素が全て検出できるまで待機する
        wait.until(EC.presence_of_all_elements_located)
        # <span class="c-lbl-sup">FX取引スタート</span>
        # //*[@id="home_depositGraph"]/section[1]/div/div/ul/li[1]/p/button[1]/span
        button1 = driver.find_element(By.LINK_TEXT, "FX取引スタート")
        button1.click()
        time.sleep(40)
    # エラーが発生した時はエラーメッセージを吐き出す。
    except Exception as e:
        print(e)
        print("エラーが発生しました。")
    # 最後にドライバーを終了する
    finally:
        # Chromeを閉じる
        driver.close()
        driver.quit()

if __name__ == '__main__':
    with ThreadPoolExecutor(max_workers=8) as executor:
        executor.submit(openSBI)

↓ここからpip list

Package             Version
------------------- ---------------
async-generator     1.10
attrs               21.4.0
certifi             2022.6.15
cffi                1.15.1
charset-normalizer  2.1.0
chromedriver-binary 103.0.5060.53.0
cryptography        37.0.4
h11                 0.13.0
idna                3.3
outcome             1.2.0
pip                 22.1.2
pycparser           2.21
pyOpenSSL           22.0.0
PySocks             1.7.1
python-dotenv       0.20.0
requests            2.28.1
selenium            4.3.0
setuptools          58.1.0
sniffio             1.2.0
sortedcontainers    2.4.0
trio                0.21.0
trio-websocket      0.9.2
urllib3             1.26.10
webdriver-manager   3.8.1
wsproto             1.1.0

1 件の回答 1

1

"FX取引スタート"というリンクテキストを持った要素が存在しないというエラーが出ています。

③「Message: no such element」は「一致する要素が存在しない」という趣旨のエラーです。

LINK_TEXTは完全一致を意味するので、PATIAL_LINK_TEXTで部分一致を探す、もしくは「FX」が半角を全角を間違えているなど。その辺りを見直すと解決できると思います。

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。