[英]selenium xpath unable to locate text on web page
我正在尝试单击 IMDB 网站上我的帐户中的“包容性”按钮,如下图所示。
我尝试了 selenium xpath 值的各种组合,并搜索了各种链接以尝试实现这一点,包括这个。 我当前的 python 低于登录 IMDB 网站以实现此更改。
driver.get('https://www.imdb.com/title/tt14772170/?ref_=nv_sr_srsg_0')
wait = WebDriverWait(driver, 20)
# click on arrow button in bottom right hand corner of image further below
# to call up pop-up window wth lists like Inclusive
xpath = "//button[@class='ipc-split-button__iconBtn']"
wait.until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
try:
match_found = True
xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false']"
button1 = driver.find_elements(By.XPATH, xpath)
xpath = "//*[contains(text(), 'Inclusive')]"
button2 = driver.find_element(By.XPATH, xpath)
xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false']/*[contains(text(), 'Inclusive')]"
button3 = driver.find_element(By.XPATH, xpath)
driver.execute_script("arguments[0].click();", button3)
except selenium.common.exceptions.NoSuchElementException:
match_found = False
按钮 1 和 2 工作,因为我想说明我需要的按钮 3 的实际 xpath 也应该工作。 最终代码中不需要按钮 1 和 2,它们只是为了说明它们至少是分开工作的。 我试图抓取的 HTML 的示例如下:
<div class="sc-1aecbe70-0 dpbfLr">
<div role="button" tabindex="0" data-titleinlist="false" class="sc-1aecbe70-1 fkjUqe">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--add sc-1aecbe70-2 csaBFR" id="iconContext-add" viewBox="0 0 24 24" fill="currentColor" role="presentation">
<path d="M18 13h-5v5c0 .55-.45 1-1 1s-1-.45-1-1v-5H6c-.55 0-1-.45-1-1s.45-1 1-1h5V6c0-.55.45-1 1-1s1 .45 1 1v5h5c.55 0 1 .45 1 1s-.45 1-1 1z"></path>
</svg>
Inclusive</div>
<a href="/list/ls560615425/?ref_=tt_ov_ls_menu_sm" aria-label="Go to list: Inclusive" class="sc-1aecbe70-4 grpYsz">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--chevron-right" id="iconContext-chevron-right" viewBox="0 0 24 24" fill="currentColor" role="presentation">
<path fill="none" d="M0 0h24v24H0V0z"></path><path d="M9.29 6.71a.996.996 0 0 0 0 1.41L13.17 12l-3.88 3.88a.996.996 0 1 0 1.41 1.41l4.59-4.59a.996.996 0 0 0 0-1.41L10.7 6.7c-.38-.38-1.02-.38-1.41.01z"></path>
</svg>
</a>
</div>
python 当前不产生错误消息,但从 button3 = driver.find_element(By.XPATH, xpath) 跳转到 except 子句
我正在使用 PyCharm 2022.2.2 Build #PC-222.4167.33 和 Python 3.10.7 和 chromedriver win32 105.0.5195.52
我之前问过类似的问题; 但这与节目分级有关,该节目具有完全不同的 HTML 设置
您的xpath
似乎是错误的。 使用下面的xpath
点击Inclusive
按钮
//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]
所以你的代码会像
xpath = "//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]"
button3 = driver.find_element(By.XPATH, xpath)
driver.execute_script("arguments[0].click();", button3)
请提供足够的等待以使元素可见或可点击。
您也可以使用xpath
以下。
//div[@class='sc-1aecbe70-0 dpbfLr']//div[@role='button'][contains(., 'Inclusive')]
你也可以使用它。
//div[@class='sc-1aecbe70-0 dpbfLr']//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.