PythonでSeleniumを使用したChrome操作時に、「/」を含むXPATHの指定が上手くいきません。

前提

PythonでSeleniumを使用し、Chromeを操作する処理を組んでいます。
ブラウザの中に表示されているカレンダーから、選択した日付をクリックする処理です。

カレンダーの日付表記は、2日目以降は「2」「3」のように表示されていますが、
1日は「1/1」のように「月/日」のように表示されています。

発生している問題

Chrome上に下記の2つの要素があります。
※一部削除しています。

 <tbody><tr><td class="in_tbl"><font><b> 2 </b></font></td></tr>

 <tbody><tr><td class="in_tbl"><font><b> 1/1 </b></font></td></tr>

下記のように、XPATHを指定してクリックする処理を組んでいます。
「x = 2」の場合はクリックされますが、
「x = 1/1」の場合はクリックされません。

x = 2 driver.execute_script('arguments[0].click();', driver.find_element(By.XPATH, f"//*[tbody/tr[1]/td/font/b={x}]"))

「/」を含むとクリックできない事象について、原因と対処方法をご教授願います。

試したこと、エラーメッセージ

  • 「x = 1/1」を指定した場合のエラー
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[tbody/tr[1]/td/font/b=1.0]"}
  • 「x = "1/1"」を指定した場合のエラー
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[tbody/tr[1]/td/font/b=1/1] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[tbody/tr[1]/td/font/b=1/1]' is not a valid XPath expression.
  • 「x = str(1/1)」を指定した場合のエラー
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[tbody/tr[1]/td/font/b=1.0]"}
  • 変数を使用せず、以下のように直接指定した場合のエラー
  • driver.execute_script('arguments[0].click();', driver.find_element(By.XPATH, "//*[tbody/tr[1]/td/font/b=1/1]"))
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[tbody/tr[1]/td/font/b=1/1] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[tbody/tr[1]/td/font/b=1/1]' is not a valid XPath expression.
  • 2月のカレンダーを表示し、「x = 2/1」を指定した場合
エラーメッセージは表示されず、カレンダーの2日目がクリックされる。 該当箇所の要素は下記の通り <tbody><tr><td class="in_tbl"><font><b> 2 </b></font></td></tr>

コメントを投稿

0 コメント