简体   繁体   English

Selenium Python 无法定位元素

[英]Selenium Python Unable to locate element

I'm trying to gather price information for each product variation from this web page: https://www.safetysign.com/products/7337/ez-pipe-marker我正在尝试从此网页收集每种产品变体的价格信息: https : //www.safetysign.com/products/7337/ez-pipe-marker

I'm using Selenium and FireFox with Python 3 and Windows 10.我在 Python 3 和 Windows 10 上使用 Selenium 和 FireFox。

Here is my current code:这是我当前的代码:

driver = webdriver.Firefox()
driver.get('https://www.safetysign.com/products/7337/ez-pipe-marker')
#frame = driver.find_element_by_class_name('product-dual-holder')
# driver.switch_to.frame('skuer5c866ddb91611')
# driver.implicitly_wait(5)
driver.find_element_by_id('skuer5c866ddb91611-size-label-324').click()   
price = driver.find_element_by_class_name("product-pricingnodecontent product-price-content").text.replace('$', '')
products.at[counter, 'safetysign.com Price'] = price
print(price)
print(products['safetysign.com URL'].count()-counter)

So, I'm trying to start by just selecting the first product variation by id (I've also tried class name).因此,我试图从仅通过 id 选择第一个产品变体开始(我也尝试过类名)。 But, I get an Unable to locate element error.但是,我收到无法定位元素错误。 As suggested in numerous SO posts, I tried to change frames (even though I can't find a frame tag in the html that contains this element).正如许多 SO 帖子中所建议的那样,我尝试更改框架(即使我在包含此元素的 html 中找不到框架标记)。 I tried switching to different frames using index, class name, and id of different div elements that I thought might be a frame, but none of this worked.我尝试使用我认为可能是框架的不同 div 元素的索引、类名和 id 切换到不同的框架,但这些都不起作用。 I also tried using waits, but that return the same error.我也尝试使用等待,但返回相同的错误。

Any idea what I am missing or doing wrong?知道我错过了什么或做错了什么吗?

To locate the elements you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can create a List and iterate over it to click() each item and you can use the following solution:要定位元素,您必须为visibility_of_all_elements_located()引入WebDriverWait ,您可以创建一个List并对其进行迭代以click()每个项目,您可以使用以下解决方案:

  • Code Block:代码块:

     from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver=webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe') driver.get("https://www.safetysign.com/products/7337/ez-pipe-marker") for product in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//form[@class='product-page-form']//div[@class='sku-contents']//following::ul[1]/li//label[starts-with(@for, 'skuer') and contains(., 'Pipe')]"))): WebDriverWait(driver, 20).until(EC.visibility_of(product)).click() driver.quit()

They may well be dynamic.它们很可能是动态的。 Select by label type selector instead and index to click on required item eg 0 for the item you mention (first in the list).改为按label类型选择器选择,然后索引以单击所需的项目,例如您提到的项目的 0(列表中的第一个)。 Also, add a wait condition for label s to be present.另外,为label存在添加等待条件。

If you want to limit to just those 5 size choices then use the following css selector instead of label :如果您只想限制为这 5 种size选择,请使用以下 css 选择器而不是label

.sku-contents ul:nth-child(3) label

ie IE

sizes = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".sku-contents ul:nth-child(3) label")))
sizes[0].click()

After selecting size you can grab the price from the price node depending on whether you want the price for a given sample size eg 0-99.选择大小后,您可以从价格节点获取价格,具体取决于您是否需要给定样本大小的价格,例如 0-99。

To get final price use:要获得最终价格,请使用:

.product-under-sku-total-label

Code:代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://www.safetysign.com/products/7337/ez-pipe-marker'
driver = webdriver.Chrome()
driver.get(url)
labels = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "label")))
labels[0].click()
price0to99 = driver.find_element_by_css_selector('.product-pricingnodecontent').text
priceTotal = driver.find_element_by_css_selector('.product-under-sku-total-label').text
print(priceTotal, price0To99)
# driver.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM