首页 > 解决方案 > Selenium Python, send number input value to li in HTML

问题描述

I am automating a script that selects a product quantity, but this particular site uses +/- click buttons and has no dropdown or input field for the quantities that are read from CSV.

Rather than emulating button presses of the +/- for the quantities, you can manually edit the HTML below with the desired number and click the add to cart for the same effect.

Is it possible to edit the HTML when there is no form to do so on the site? Or do I have to emulate clicks for the desired quantity. I am using Pandas to read the CSV and is working code from another project.

Thank you if you can help

https://www.matsukiyo.co.jp/store/online/p/4902888248269

<ul class="count">
        <li>数量</li>
        <li class="minus disable"><a href="#">-&lt;/a></li>
        <li class="num" id="addToCartQty" data-code="99">1</li>
        <li class="plus"><a href="#">+&lt;/a></li>
        </ul>

You can change the data-code="99">1</li> from 1 to 99 and it will update but is this possible in Selenium as I cannot find the right selector to do so. I have a feeling this is not possible?

The current code I am trying is sending clicks but I am not able to send the number of clicks I want and am stuck for a solution either way.

How can I send the value from the CSV to dictate the number of clicks? Or how can I write the value to the li HTML?

driver.get(web_url+str(UPC[i]))
try:
    driver.find_element(By.LINK_TEXT, "+&quot;).click
    # I want to add the number of clicks here(str(quantity[i]))
    link = driver.find_element_by_css_selector("p.button.cart")
    link.click()
except:
    print("Product Number: "+str(UPC[i])+" Sold Out!")

标签: pythonselenium

解决方案


我可以看到包含计数的元素位于 id='addToCartQty' 所以你可以做的很简单

driver.find_element_by_css_selector('#addToCartQty').sendKeys("your value");

或者

driver.find_element_by_css_selector('#addToCartQty').sendKeys("value", "your value");

或者

element = driver.find_element_by_css_selector('#addToCartQty')
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value='enter the value here';", element);

不要用您希望在此处输入的数字伪造更改“您的值”或“在此处输入值”。


推荐阅读