Using Selenium library for updating products in WooCommerce? Yes it’s possible…
Selenium is a popular library made for internet apps browser tests. But are there other ways of using it? Sure, Selenium automates browsers. It can do nearly anything you can do in a web browser. Let’s imagine that you have a big e-commerce site with hundreds of products. You need to change all the prices each month. Doing it manually is time-consuming work . Let’s use Selenium for this job. We can write simple script once and use it each time while we will need update our prices.
I will use C#, but in Selenium you can also use Java, Python, Javascript, Kotlin or Ruby.First, we need log in automatically with Selenium to our webshop:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://yourwebsite.com");
//clicking Rodo Accept
IWebElement searchfieldRODO = driver.FindElement(By.CssSelector("[id='cn-accept-cookie"));
searchfieldRODO.Click();
//admin parts
//logging to WP admin part
IWebElement login = driver.FindElement(By.CssSelector("[id='user_login']"));
string loginentry = "your login";
login.SendKeys(loginentry);
IWebElement password = driver.FindElement(By.CssSelector("[id='user_pass']"));
string passentry = "your password*";
password.SendKeys(passentry);
IWebElement SubmitLogin = driver.FindElement(By.CssSelector("[id='wp-submit']"));
SubmitLogin.Click();
And now Selenium is logged to WP admin panel. You need data collection to enter. You can use various formats of data(json, dataframe, even google Sheets…). In this case, I used a simple .net generic collection List<type>. My list has a number of string-type tables – item ID and price.
List<string[]> priceData2 = new List<string[]>();
priceData2.Add(new string[2] { "020z", "475" });
priceData2.Add(new string[2] { "4947", "564" });
priceData2.Add(new string[2] { "04948", "180" });
priceData2.Add(new string[2] { "04949", "036" });
priceData2.Add(new string[2] { "04951", "180" });
priceData2.Add(new string[2] { "4952", "711" });
priceData2.Add(new string[2] { "4953", "092" });
priceData2.Add(new string[2] { "4950", "918" });
priceData2.Add(new string[2] { "I204", "74" });
priceData2.Add(new string[2] { "956", "97" });
priceData2.Add(new string[2] { "04954", "135" });
We need a loop that will iterate on the operation of changing prices in WooCommerce. Simple – find the product by ID and change the price. I will use foreach c# loop.
foreach (string[]a in priceData2)
{
IWebElement Products = driver.FindElement(By.CssSelector("[id='menu-posts-product']"));
Products.Click();
IWebElement WszystkieProdukty = driver.FindElement(By.XPath("//*[@id=\"menu-posts-product\"]/ul/li[2]/a")); ;
WszystkieProdukty.Click();
IWebElement ProductSearch = driver.FindElement(By.CssSelector("[id='post-search-input']"));
string productToSearch = a[0];
ProductSearch.SendKeys(productToSearch);
IWebElement SzukaJButton = driver.FindElement(By.CssSelector("[id='search-submit']"));
SzukaJButton.Click();
IWebElement ProductToChange = driver.FindElement(By.ClassName("row-title"));
ProductToChange.Click();
IWebElement PriceField = driver.FindElement(By.CssSelector("[id='_regular_price']"));
string newPrice = a[1];
PriceField.Clear();
PriceField.SendKeys(newPrice);
IWebElement Aktualizuj1 = driver.FindElement(By.CssSelector("[id='publish']"));
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
wait.Until(d => Aktualizuj1.Enabled);
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", Aktualizuj1);
}
After this operation, prices are updated. My list in this example is short, but you can have hundreds of items and they will be actualized remotely. At the end, you can close the browser by Selenium command:
public void QuitDrive()
{
driver.Quit();
}