selenium(python)にて、リンク要素のクリック自動化が出来ない。
■起こっている現象
あるWEBサイト作業の自動化プログラムを作成しています。
ログインまではできたのですが、その次のアクション(ページ内にあるリンクをクリックする)
がうまくいきません。
■pythonのコード
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() PROFILE_PATH = r'C:\Users\motoc\AppData\Local\Google\Chrome\User Data' options.add_argument('--user-data-dir=' + PROFILE_PATH) driver = webdriver.Chrome(options=options) # ログイン含めたWEBサイトへのアクセスは成功した driver.get("https://sellercentral-japan.amazon.com/gp/homepage.html/ref=xx_home_logo_xx") # ここからが、今回の質問範囲(クリックできない部分) $driver->getElement(WebDriverBy::xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[1]"))->click(); #その他の試し1(クリックできない) # driver.find_element_by_xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[1]").click() #その他の試し2(クリックできない) # element = driver.find_element_by_link_text("未出荷") # リンクをクリック # element.click()
■HTML抜粋:
全体は非常に複雑な構成なので、該当箇所の抜粋になります。
<span class="a-declarative" data-action="myo-actionItemLink" data-myo-actionItemLink="{}"> <span> <a class="a-size-medium a-spacing-mini a-link-normal sc-widget-action-item" href="/orders-v3/mfn/unshipped/ref=bb_myo_wos3_home?_encoding=UTF8&sort=status_desc"> <div class="sc-widget-action-item-value"> <span class="a-text-bold">0</span> </div> <div class="sc-widget-action-item-title">未出荷</div> </a> </span> </span>
■エラー内容
C:\Users\motoc>python google-search3.py File "google-search3.py", line 14 $driver->getElement(WebDriverBy::xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[1]"))->click(); ^ IndentationError: unexpected indent C:\Users\motoc>python google-search3.py File "google-search3.py", line 14 $driver->getElement(WebDriverBy::xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a"))->click(); ^ IndentationError: unexpected indent C:\Users\motoc>python google-search3.py File "google-search3.py", line 14 $driver->getElement(WebDriverBy::xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[2]"))->click(); ^ IndentationError: unexpected indent C:\Users\motoc>
■デベロッパーツールで、xpathをしらべた結果、3つの候補がでてきましたので、すべてトライしました。
//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[2] //*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a //*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[1]
エラーのIndentationError: unexpected indentは、(要素の取得以前に)ソースのインデントが正しくないという意味なのですが、これがソースの全文なのでしょうか?
申し訳有りません。やってはいけない過ちをしてしまいました。見やすくするために、ソース全体を掲載しておりませんでした。
ソース全体は、以下の通りです。
ーーーーーーーーーーーーーーーーーーここからーーーーーーーーーーーーーーーーーー
# seleniumを使う準備
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
PROFILE_PATH = r'C:\Users\motoc\AppData\Local\Google\Chrome\User Data'
options.add_argument('--user-data-dir=' + PROFILE_PATH)
driver = webdriver.Chrome(options=options)
driver.get("https://sellercentral-japan.amazon.com/gp/homepage.html/ref=xx_home_logo_xx")
# url = driver.find_element_by_xpath('//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[2]').get_attribute('href')
# driver.get(url)
$driver->getElement(WebDriverBy::xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[2]"))->click();
# //*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[2]
# //*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a
# //*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[1]
# driver.find_element_by_xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[1]").click()
#XPATHは自分がクリックしたいXPATH
# element = driver.find_element_by_link_text("未出荷")
# 画像のリンクをクリック
# element.click()
# driver.find_element_by_link_text("プライム未出荷").click()
# element = driver.find_element_by_link_text("プライム未出荷")
# 画像のリンクをクリック
# element.click()
# driver.findElement(By.xpath("//*[text()=\"注文管理\"]")).click();
# driver.findElement(By.xpath("//*[text()=\"未出荷\"]")).click();
# driver.findElement(By.linkText("プライム未出荷")).click();
#注文管理 <a href="/order-reports-and-feeds/reports/ref=xx_orderrpt_dnav_xx">
私はPythonで文頭・文末に$や;を持ってくる文法をしらないのですが、その他の試し1やその他の試し2のの場合も、同じエラーメッセージなのでしょうか。
frederick_1974さんに便乗させていただきます。
frederick_1974さんのご指摘の通りで、
$driver->getElement(WebDriverBy::xpath...) のあたり、これはPHPの文法ではないでしょうか?
ここをコメントアウトして、 driver.find_element_by_xpath(...) (pythonのWebdriver apiの書き方)の方で試されるといいのでは。
いかがでしょうか?
ありがとうございます。
ご指摘いただきました内容に基づき、修正しました。
エラーがでています。
ーーーーーーエラー内容ーーーーーーーー
C:\Users\motoc>python google-search3.py
File "google-search3.py", line 14
driver.findElement(By.xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a")).click()
^
SyntaxError: invalid syntax
C:\Users\motoc>
ーーーpythonーーーーーーーー
# seleniumを使う準備
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
PROFILE_PATH = r'C:\Users\motoc\AppData\Local\Google\Chrome\User Data'
options.add_argument('--user-data-dir=' + PROFILE_PATH)
driver = webdriver.Chrome(options=options)
driver.get("https://sellercentral-japan.amazon.com/gp/homepage.html/ref=xx_home_logo_xx")
# driver.findElement(By.xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a")).click()
# 一番最後の行で、エラーがでていた為、デベロッパーツールでのコピーに余計な空白文字などあるかもしれないと思い、最終行は、上の行を手動入力しなおした。
driver.findElement(By.xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a")).click()
「SyntaxError: invalid syntax」は文法が誤っていることを指しています。
上でsuamaさんも書かれているとおり、エレメントの指定の仕方は、
driver.findElement(By.xpth())ではなく、driver.find_element_by_xpath()となります。
そのため、もとの質問文にある、
#その他の試し1(クリックできない)
#driver.find_element_by_xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a/div[1]").click()
のコメントアウトを外して試してみてはいかがでしょうか。
ありがとうございます。だめですね。
ーーーーーーーーーエラーーーーーーーーーー
C:\Users\motoc>python google-search3.py
File "google-search3.py", line 12
driver.find_element_by_xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a").click()
^
SyntaxError: invalid syntax
C:\Users\motoc>
ーーーーーーーーpythonーーーーーーーーーーーーー
# seleniumを使う準備
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
PROFILE_PATH = r'C:\Users\motoc\AppData\Local\Google\Chrome\User Data'
options.add_argument('--user-data-dir=' + PROFILE_PATH)
driver = webdriver.Chrome(options=options)
driver.get("https://sellercentral-japan.amazon.com/gp/homepage.html/ref=xx_home_logo_xx")
#その他の試し1
driver.find_element_by_xpath("//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a").click()
失礼しました。ダブルクォーテーション(")が二つ目が現れたところで、そこまで("//*[@id=")文字として認識していますので、xpath()のカッコの中の最初と最後をシングルクォーテーション(')にし、シングルクォーテーションとダブルクォーテーションの入れ子にすれば、少なくともSyntaxErrorは発生しないはずです。(要素をクリックできるかは別として)
driver.find_element_by_xpath('//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a').click()
ありがとうございます。
ありがとうございます。
確認させていただきます。
(パソコンの電源が切れそうなので、あとになります。)
ありがとうございます!!
できました。
ーーーーーーーpythonファイルーーーーーーーーーーーー
# seleniumを使う準備
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
PROFILE_PATH = r'C:\Users\motoc\AppData\Local\Google\Chrome\User Data'
options.add_argument('--user-data-dir=' + PROFILE_PATH)
driver = webdriver.Chrome(options=options)
driver.get("https://sellercentral-japan.amazon.com/gp/homepage.html/ref=xx_home_logo_xx")
#その他の試し1
driver.find_element_by_xpath('//*[@id="widget-fxmXCT"]/div/div[2]/div[1]/span[4]/span/a').click()
frederick_1974様
suama様
年始のお忙しい時に、御時間を割いて頂き、本当に、ありがとうございました。
感謝申し上げます。
回答1件
あなたの回答
tips
プレビュー