Python,Selenium 试图通过 class xpath 定位图像
Python, Selenium trying to target image by class xpath
我试图通过 python/selenium 单击页面上出现的第一张图片。
html代码是:
<div class="flex-none ph1 mt2 w-sixth-ns w-third">
<a class="aspect-ratio--1x1 relative db bg-near-black hover-bg-dark-primary bg-animate outline-0 no-underline overflow-hidden br1" href="https://users/792647/pictures/88860858" title="Random name cannot use this to link in selenium. ">
<div class="aspect-ratio--object">
<div class="pa2 flex items-center justify-center h-100 w-100">
<img alt="Random name cannot use this to link in selenium" class="db w-100 h-100 ipp object-cover" src="https://pic-c400-3.com/792647/00059768-ff6a-1304-0f8b-4060f001e784/c400.jpg?token=1578873600_78bae2898bc20e48508cdf6640fe7295ba0c429b85c57019936be6864df471e4">
</div>
</div>
</a>
</div>
我试过的代码,都不起作用:
photo = browser.find_elements_by_xpath('//*[@class="pa2 flex items-center justify-center h-100 w-100"]').click()
和:
photo = browser.find_elements_by_xpath('//*[@class="flex-none ph1 mt2 w-sixth-ns w-third"]').click()
诱导 WebDriverWait
() 并等待 element_to_be_clickable
() 并使用以下 xpath 选项。
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(@class,'aspect-ratio') and contains(@href,'https://users')]//img[1]"))).click()
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
当您尝试 click()
使用 's python client you to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following 出现在页面上的第一张图片时:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='pictures']>div.aspect-ratio--object img.object-cover[src^='https://pic']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'pictures')]/div[@class='aspect-ratio--object']//img[contains(@class,'object-cover') and starts-with(@src, 'https://pic')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我试图通过 python/selenium 单击页面上出现的第一张图片。
html代码是:
<div class="flex-none ph1 mt2 w-sixth-ns w-third">
<a class="aspect-ratio--1x1 relative db bg-near-black hover-bg-dark-primary bg-animate outline-0 no-underline overflow-hidden br1" href="https://users/792647/pictures/88860858" title="Random name cannot use this to link in selenium. ">
<div class="aspect-ratio--object">
<div class="pa2 flex items-center justify-center h-100 w-100">
<img alt="Random name cannot use this to link in selenium" class="db w-100 h-100 ipp object-cover" src="https://pic-c400-3.com/792647/00059768-ff6a-1304-0f8b-4060f001e784/c400.jpg?token=1578873600_78bae2898bc20e48508cdf6640fe7295ba0c429b85c57019936be6864df471e4">
</div>
</div>
</a>
</div>
我试过的代码,都不起作用:
photo = browser.find_elements_by_xpath('//*[@class="pa2 flex items-center justify-center h-100 w-100"]').click()
和:
photo = browser.find_elements_by_xpath('//*[@class="flex-none ph1 mt2 w-sixth-ns w-third"]').click()
诱导 WebDriverWait
() 并等待 element_to_be_clickable
() 并使用以下 xpath 选项。
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(@class,'aspect-ratio') and contains(@href,'https://users')]//img[1]"))).click()
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
当您尝试 click()
使用 element_to_be_clickable()
and you can use either of the following
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='pictures']>div.aspect-ratio--object img.object-cover[src^='https://pic']"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'pictures')]/div[@class='aspect-ratio--object']//img[contains(@class,'object-cover') and starts-with(@src, 'https://pic')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC