How to click on this web element using selenium webdriver? - html

HTML
How would you click on this particular item in selenium each time by having just the date you want as a variable? I have shown the item in the attached image of the HTML. It is a panel calendar, however, I can't find the item using by.ID, class, etc. I can Click on it by using XPath however how do I make it specific to the data? I would like to click on it based on the.
data-moment="2022-10-30"
currently I just have this:
### 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(5)
driver.find_element(By.XPATH,"//*[#id='gz-time-slot-calendar']/ul/li[4]/a").click()
pause(10)

Change your XPath to this:
data_moment = "2022-10-30"
driver.find_element(By.XPATH,f"//*[#id='gz-time-slot-calendar']//a[#data-moment='{data_moment}']").click()

Related

Finding Next Select Control on HTML page using Selenium in VB.net

On my Webpage I have three Dropdown Controls. When First Option is selected in the First Combo, the Second Combo Control Gets Populated and similarly on selecting an Option in the Second Combo populates the Third Combo Control.
Each Combo Control has a above it. Using Selenium in VB.net, I am able to find the First Combo Control and iterate through each Option Available. The Challenge I am facing is that, the label on Second and third Combo gets its text from the the Selected Option of the First and Second Combos, respectively. However, the XPath changes randomly with each option selected due to which the FindElement(By.Xpath) fails.
The example Combos are available this Link Set of Three Combos
I tried using the following VB.net Code
Dim CWEstring = "./following-sibling::select"
GE = New SelectElement(driver.FindElement(By.XPath(CWEstring)))
and
Dim GEString = "./following-sibling::select"
GE = New SelectElement(driver.FindElement(By.XPath(GEString )))
I did my research for few days but couldn't get the answer, so posting here for help.
Use the following xpath to identify the element.
1st dropdwon:
//select[#id='edit-field-select-zone2-value']
2nd Dropdown:
//select[#id='edit-field-cce-army-no-1dinjan-cwe-tid']
3rd dropdown:
//select[#id='edit-field-pm-khonsa-tid']
Please note you have to provide some wait after selecting each dropdown item to appear next dropdown on the webpage.
I Downloaded the webpage Source and Edited the code to bear minimum HTML content so that all three Combo Controls are visible and save the html content as a .htm file on local disk.
Using chrome I opened the .htm file. Then Using MS-Excel From Web under Data tab Scraped the data from the local .htm file.
Now I had all values under one Column Header name. I converted all values under column name into an array.
Using VB.net and Selenium technology, I iterated through each combo control and saved the data to a New Excel File.
More Challenges came to fore. Therefore, I used Try...Catch method to trap the error and Continue the Iterations (Loops).
And now I have all the values the way I wanted them.

Is there an error in my XPath locator for the provided HTML?

I am currently trying to type a filter into a search bar and then select the option that comes up in a field. My understanding is that by sending the keys to filter the selection I would be able to click the result. I have tried these three different lines of code to select the option 'Date of Donation' and am having no luck. The errors I get are that the element was not in scroll view or a timeout error when it cannot be located. Is there some other way to locate this option and then double click on it?
import selenium
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Firefox()
driver.find_element_by_xpath("//option[contains(text(),'Date of Donation')]").click()
driver.find_element_by_xpath("//*[id='fieldsDisplay']/optgroup[6]/option[4]")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div/div[5]/form/div[1]/table[2]/tbody/tr[3]/td[2]/select[2]/optgroup[6]/option[4]")))
The HTML for this element of the form looks as such:
<option value="7313" fieldgroupid="373" fieldid="7313" fieldtype="DATE">
Date of Donation
</option>
I've included a picture of the form I am trying to access for further clarity:
screenshot
I would try scrolling to the element with Javascript first, and see if that helps:
# locate the element
date_of_donation = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//option[contains(text(), 'Date of Donation')]")))
# scroll into view before clicking
driver.execute_script("arguments[0].scrollIntoView(true);", date_of_donation)
# now click
driver.execute_script("arguments[0].click();", date_of_donation)
It's also possible that this element exists within an iframe, in which case it would need to be switched to before interacting with the element.

Find HTML span element

HTML screenshotI am trying to build a test case to select an option from the drop down menu but unable to find the element.
I am new to selenium webdriver module I can find elements using xpath and id but unable to find an element from a drop down menu.
I am able to log into the webpage and click "Queues". Now I want to select "All Items" from the queue but gettig errow
All Items
If you are not able to find it by selenium and exception is throwing like element not found that probably there is a frame present and your element is inside it. you need to switch to the frame first.
You can refer my or other answer in below link:
How to select frame if there are multiple frames on a webpage and get content of that frame?
If you are not able to find in HTML then use below code
System.out.println(driver.getPageSource());
It just going to print your HTML, now copy it in notepad++ and search your dropdown Text. now create Xpath

Is it true that DIV elements cannot be clicked with selenium Web Driver?

Is it true that DIV elements cannot be clicked with selenium Web Driver?
For Instance, I'm unable to click on delete button in Gmail
Am trying to locate the elemnt using XPATH = //div[#aria-label='Delete']
here, class names and id's are dynamic which changes for every session. Imean, for every Login and Logout. I want my script to be Robust to run at any time.
You can by class name or ID, e.g. with class:
driver.findElement(By.className("class")).click();
Or by element name:
driver.findElement(By.ByTagName("div")).click();
Or find the parent or child a tag.
Have you tried using a different attribute ?
For e.g. you can use
//div[#data-tooltip='Delete']
or
//div[#data-tooltip='Delete']/div/div
It may derive because of gmail has async structure. You may wait to finish request after page load and after select action. If you can share code flow we can examine together.

Convert Selenium IDE to Python

When I click a button using Selenium IDE I get 2 results in the "Table" tab:
selectFrame {"id":"3f923370-2dae-80d8-f36f-670e9caa7f7d","containerVersion":"7-GA","webContextPath":"/mmm"}
and
click xpath=(//button[#type='button'])[2]
How do I turn this into usable code in python? Using driver.switch_to_frame and driver.find_element_by_xpath ??
You need to first switch to the specific frame and then search for the element in the frame. You can try this:
driver.switch_to_frame(driver.find_element_by_id("3f923370-2dae-80d8-f36f-670e9caa7f7d")
driver.find_element_by_xpath("//button[#type='button'][2]").click()
I'm not familiar with Selenium IDE but I'm assuming that 3f923370-2dae-80d8-f36f-670e9caa7f7d is the id attribute value of the frame tag.