I would like to know how to loop through things in Selenium. Basically, I just want to make a for-loop to find all the "panel titles" that have earlier than a certain time and then an if statement that says if it has 4 golfers then I will click the book button.
I'm just not sure how you iterate through the xpaths to these things in HTML. All I have been able to do so far is just click on the correct Day with my code below, however I can't figure out
### Gets Edge driver and doesnt need extension update
driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))
driver.get(
'https://teewire.net/granada/'
)
driver.maximize_window()
pause(2)
data_moment = "2022-10-30"
driver.find_element(By.XPATH,f"//*[@id='gz-time-slot-calendar']//a[@data-moment='{data_moment}']").click()
pause(5)
data_moment = "2022-10-30"
driver.find_element(By.XPATH,f"//*[@id='gz-time-slot-calendar']//a[@data-moment='{data_moment}']").click()
pause(5)
a = driver.find_elements(By.XPATH,"//*[@id='time-slots-container-id']//a[@class='{'panel-heading'}']")
pause(2)
for i in a:
print(i.text)
Here's an attached file. HTML
CodePudding user response:
In the documentation I found this:
driver.find_elements(By.XPATH, '//button')
https://selenium-python.readthedocs.io/locating-elements.html
a quick example I wrote up is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.theregister.com/")
elements = driver.find_elements(By.XPATH, '//article')
first = elements[0]
CodePudding user response:
Try this code:
golfers_text = driver.find_elements(By.CSS_SELECTOR, ".panel.panel-default.teetime .panel-body")
i = 0
for golfer_text in golfers_text:
value = golfer_text.text
print(value)
if "Up to 4 golfers" in value:
driver.find_element(By.XPATH, "(//*[@class='panel panel-default teetime']//div[@class='panel-body']/button)[" str(i 1) "]").click()
break
print("------")
i = 1