If I can found the value of class="week", how to get data-title for specified value. See…
- User
If I can found the value of class="week", how to get data-title for specified value. See the example of code. If I type class="week" = 5 and value is 6 I need r1c4. Write selenium Python code
- Assistant
To achieve this using Selenium with Python, you can use the following code:
from selenium import webdriver # Instantiate the WebDriver (e.g., Chrome) driver = webdriver.Chrome() # Navigate to the webpage containing the desired HTML code driver.get("your_website_url") # Find all elements with class="week" weeks = driver.find_elements_by_class_name("week") # Iterate through each week element for week in weeks: # Check if the week value matches your desired value if week.text == "5": # Find the sibling elements with class="available" and retrieve their data-title values cells = week.find_elements_by_xpath("./following-sibling::td[@class='available']") for cell in cells: data_title = cell.get_attribute("data-title") if cell.text == "6": print(data_title) break # Close the WebDriver driver.quit()Make sure to replace
"your_website_url"with the actual URL of the webpage you are scraping. - User
AttributeError Traceback (most recent call last) Cell In[21], line 2 1 # Find all elements with class=“week” ----> 2 weeks = driver.find_elements_by_class_name("week")
AttributeError: 'WebDriver' object has no attribute 'find_elements_by_class_name'
- Assistant
Apologies for the confusion. It seems that the WebDriver instance does not have the
find_elements_by_class_namemethod directly. However, you can still achieve the desired result by using other locator strategies. Here's an updated code snippet:from selenium import webdriver # Instantiate the WebDriver (e.g., Chrome) driver = webdriver.Chrome() # Navigate to the webpage containing the desired HTML code driver.get("your_website_url") # Find all td elements td_elements = driver.find_elements_by_tag_name("td") # Iterate through each td element for td in td_elements: # Check if the td element has class="week" and value is 5 if td.get_attribute("class") == "week" and td.text == "5": # Find the sibling elements with class="available" and retrieve their data-title values siblings = td.find_elements_by_xpath("./following-sibling::td[contains(@class, 'available')]") for sibling in siblings: data_title = sibling.get_attribute("data-title") if sibling.text == "6": print(data_title) break # Close the WebDriver driver.quit()Again, it's important to replace
"your_website_url"with the actual URL of the webpage you're working with. - User
AttributeError Traceback (most recent call last) Cell In[22], line 2 1 # Find all td elements ----> 2 td_elements = driver.find_elements_by_tag_name("td")
AttributeError: 'WebDriver' object has no attribute 'find_elements_by_tag_name'
- Assistant
Apologies for the oversight. The error occurred because I mistakenly referenced the
find_elements_by_tag_namemethod on thedriverobject instead of its correspondingdriver.find_elements_by...method.Here's the corrected code:
from selenium import webdriver # Instantiate the WebDriver (e.g., Chrome) driver = webdriver.Chrome() # Navigate to the webpage containing the desired HTML code driver.get("your_website_url") # Find all td elements td_elements = driver.find_elements_by_css_selector("td") # Iterate through each td element for td in td_elements: # Check if the td element has class="week" and value is 5 if td.get_attribute("class") == "week" and td.text == "5": # Find the sibling elements with class="available" and retrieve their data-title values siblings = td.find_elements_by_xpath("./following-sibling::td[contains(@class, 'available')]") for sibling in siblings: data_title = sibling.get_attribute("data-title") if sibling.text == "6": print(data_title) break # Close the WebDriver driver.quit()Please ensure that you replace
"your_website_url"with the actual URL of the webpage you're working with.