python-3.x 当XPATH包含撇号时如何通过XPATH查找

g0czyy6m  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(145)

enter image description here所以我试图通过XPATH查找,它包含一个撇号。所以,我切换到调用这些ID代替,其中一个工作的第二个不。所以,我尝试再次通过路径调用它,这次在两个位置都切换了单引号和双引号。它适用于ID gen 31,但不适用于ID gen 140。

完整代码供参考

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
edge_options = Options()
edge_options.add_experimental_option("detach", True)

driver = webdriver.Edge(edge_options)
driver.implicitly_wait(20)
driver.get("https://oceanspray-amc.ivanticloud.com/Account/SelectRole?returnUrl=%3FNoDefaultProvider%3DTrue&NoDefaultProvider=True/")    
driver.maximize_window()

#log in using azure AD
driver.find_element(By.XPATH, "/html/body/div[3]/div/form/div[4]/div/a").click();
#Service Desk Analyst
driver.find_element(By.XPATH, "/html/body/div[3]/div/form/div[3]").click();
#Submit Button
driver.find_element(By.XPATH, "/html/body/div[3]/div/form/div[4]/div/button").click();
#Service Request Button
driver.find_element(By.ID, "ext-gen31").click();

time.sleep(2)
#New Service Request Button

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id=\"ext-gen140\"]"))).click();

driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div/div/div/div[1]/div/div/div[2]/div/div[1]/div/div[2]/div/div/div/table/tbody/tr/td[1]/table/tbody/tr/td[1]/table/tbody/tr[2]/td[2]/em/button").click();

# driver.find_element(By.XPATH, "//*[@id=\"ext-gen140\"]").click()

# driver.find_element(By.ID, "ext-gen140").click();


PS C:\Users\msmith2\Desktop\Work> & C:/Users/msmith2/AppData/Local/Microsoft/WindowsApps/python3.11.exe "c:/Users/msmith2/Desktop/Work/Ivanti Auto.py"
[8840:15872:0830/012912.136:ERROR:chrome_browser_cloud_management_controller.cc(163)] Cloud management controller initialization aborted as CBCM is 
not enabled.
[8840:15872:0830/012912.164:ERROR:assistance_home_client.cc(32)] File path C:\Users\msmith2\AppData\Local\Temp\scoped_dir17404_1882902945\Default

DevTools listening on ws://127.0.0.1:50390/devtools/browser/ec2311c1-7749-49e9-8714-1ff0f21680fe
[8840:15872:0830/012913.317:ERROR:fallback_task_provider.cc(124)] Every renderer should have at least one task provided by a primary task provider. 
If a "Renderer" fallback task is shown, it is a bug. If you have repro steps, please file a new bug and tag it as a dependency of crbug.com/739782. 
[8840:15872:0830/012916.194:ERROR:fallback_task_provider.cc(124)] Every renderer should have at least one task provided by a primary task provider. 
If a "Renderer" fallback task is shown, it is a bug. If you have repro steps, please file a new bug and tag it as a dependency of crbug.com/739782. 
Traceback (most recent call last):
  File "c:\Users\msmith2\Desktop\Work\Ivanti Auto.py", line 29, in <module>
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id=\"ext-gen140\"]"))).click();
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\msmith2\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
        GetHandleVerifier [0x00007FF6FA243D72+61490]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6FA1D5FC2+740594]
        (No symbol) [0x00007FF6F9FAB8AE]
        (No symbol) [0x00007FF6F9FF32F4]
        (No symbol) [0x00007FF6F9FF3405]
        (No symbol) [0x00007FF6FA02FCE7]
        (No symbol) [0x00007FF6FA01394F]
        (No symbol) [0x00007FF6F9FE7021]
        (No symbol) [0x00007FF6FA02CE21]
        (No symbol) [0x00007FF6FA0136E3]
        (No symbol) [0x00007FF6F9FE5EAA]
        (No symbol) [0x00007FF6F9FE518B]
        (No symbol) [0x00007FF6F9FE6634]
        Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF6FA408D29+1207369]
        (No symbol) [0x00007FF6FA065304]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6FA1290E1+32273]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6FA1219D9+1801]
        Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF6FA407904+1202212]
        Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6FA1DE958+19784]
        Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6FA1DAE14+4612]
        Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF6FA1DAF46+4918]
        Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF6FA1CF411+713025]
        BaseThreadInitThunk [0x00007FFF13D07614+20]
        RtlUserThreadStart [0x00007FFF140226B1+33]

而不是徘徊在

<table id="ext-comp-1050" cellspacing="0" class="x-btn   x-btn-noicon " style="width: auto;"><tbody class="x-btn-small x-btn-icon-small-left"><tr><td class="x-btn-tl"><i>&nbsp;</i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-ml"><i>&nbsp;</i></td><td class="x-btn-mc"><em class="" unselectable="on"><button type="button" id="ext-gen142" class=" x-btn-text" tabindex="-1">New</button></em></td><td class="x-btn-mr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-bl"><i>&nbsp;</i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i>&nbsp;</i></td></tr></tbody></table>

盘旋在

<table id="ext-comp-1050" cellspacing="0" class="x-btn   x-btn-noicon  x-btn-over " style="width: auto;"><tbody class="x-btn-small x-btn-icon-small-left"><tr><td class="x-btn-tl"><i>&nbsp;</i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-ml"><i>&nbsp;</i></td><td class="x-btn-mc"><em class="" unselectable="on"><button type="button" id="ext-gen142" class=" x-btn-text" tabindex="-1">New</button></em></td><td class="x-btn-mr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-bl"><i>&nbsp;</i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i>&nbsp;</i></td></tr></tbody></table>
zpqajqem

zpqajqem1#

您可以尝试像这样查找XPath(双引号内的单引号)

driver.find_element(By.XPATH, "//*[@id='ext-gen140']").click();

或使用转义序列

driver.find_element(By.XPATH, "//*[@id=\"ext-gen140\"]").click()

相关问题