python - 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="#">-</a></li>
<li class="num" id="addToCartQty" data-code="99">1</li>
<li class="plus"><a href="#">+</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, "+").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!")
解决方案
我可以看到包含计数的元素位于 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);
不要用您希望在此处输入的数字伪造更改“您的值”或“在此处输入值”。
推荐阅读
- c# - 关闭日志记录和调试
- python - 已安装 PYSWIP,但尝试导入时显示“模块不存在”
- android - RecycleView List with SearchView is empty after back press
- php - IIS PHP 包含文件停止工作,除非我删除旧的并创建新的
- java - 接收 XML 列表
- javascript - 使用 jQuery 在特定结果 div 中单击它后,如何显示所有动态单选输入值的总和?
- java - 在 Android 应用程序中看不到导入的依赖模块类
- javascript - 如何等到函数返回结果
- django - 在 Django 中,我正在尝试模板文件夹,但它不在模板文件夹中
- flutter - Flutter:通过构造函数传递数据