Reputation:
I'm testing with this site here any https://www.nike.com.br/cosmic-unity-153-169-211-324680 And I'm trying after a few seconds that the page loads you must select the size and I can't select the size automatically with Selenium. Can someone help me?
Look, when it appears for you to select the size of the sneaker, I'm in Brazil and I select the size 40 of the sneaker, only if you inspect the "40" you will see that it is a label, and this label has no id, this label is the following html code snippet:
<label for="tamanho__id40">40</label>
How could I click on this label in Selenium?
I currently have this code:
import datetime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities
import DesiredCapabilities from selenium.webdriver.support.ui
import WebDriverWait from selenium.webdriver.common.by
import By from selenium.webdriver.support
import expected_conditions as EC
import time
option = Options()
prefs = {'profile.default_content_setting_values': {'images': 2}}
option.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options = option)
# Navigate to url
driver.get"https://www.nike.com.br/cosmic-unity-153-169-211-324680")
What would I have to add to be able to click on this label that has no id?
Upvotes: 2
Views: 389
Reputation: 4212
1 You need to accept cookies
2 Use Selenium's explicit waits. To use them you will need to import:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
3 Use reliable locators. I propose using this xpath
locator for 40 shoe size: //label[@for="tamanho__id40"]
4 I added some chrome_options
for dealing with this site.
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-blink-features")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', options=chrome_options)
driver.get("https://www.nike.com.br/cosmic-unity-153-169-211-324680")
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.cc-allow'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//label[@for="tamanho__id40"]'))).click()
Upvotes: 1