how to select unique attribute of div in cypress - attributes

How do I select the unique attribute of div in Cypress? In my case, I want to get data-testid
<div id="Dropdown78"
class="ms-Dropdown-title"
data-testid= "busnum_drop">
</div>

You can use this:
cy.get('#Dropdown78').click()
cy.get('[data-testid="busnum_drop"]').click()

Related

Python Selenium, find certain elements under a certain element

<section id='browse-search'>
<div>
<div>
<div>
<div class='product-pod'>
<div class='product-pod>'>
</div>
</div>
</div>
</section>
<div class='product-pod>'>
<div class='product-pod>'>
I have a webpage like this structure. and I need a cleaner way to locate elements with class='product-pod'. driver.find_elements(By.XPATH,"div[#class='product-pod']") will not work, because there are a few matched elements outside the section element.
Please advise what is the most appropriate way to locate those elements.
With what you have provided, this strategy could be built:
if the extra > that you provided is a typo, then:
For first div element:
driver.find_element(By.XPATH, "(//section[#id='browse-search']//div[#class='product-pod'])[1]")
For second div element:
driver.find_element(By.XPATH, "(//section[#id='browse-search']//div[#class='product-pod'])[2]")
If the > is not a typo, then the structure changes, and the below strategies would work:
For main div element:
driver.find_element(By.XPATH, "//section[#id='browse-search']//div[#class='product-pod']")
For inner div element:
driver.find_element(By.XPATH, "//section[#id='browse-search']//div[#class='product-pod']/div")
you can try xpath like
//section[#id='browse-search']//div[contains(#class,'product-pod')]
which will collect all product-pod classes inside section having id = browse-search

How to find all the span tag inside of an element in selenium python?

<div id="textelem" class="random">
<span class="a">
TEXT 1
</span>
<span>
<span>TEXT 2 </span>
</span>
<span>TEXT 3</span>
</div>
Python: TargetElem = self.wait.until(EC.presence_of_element_located((By.ID, "textelem")))
I want to get all the text inside of span tags of TargetElem element. How can I get all the span elements inside of TargetElem element and loop through them to get a single string of collected text. Thank you.
simply use .text
TargetElem = self.wait.until(EC.presence_of_element_located((By.ID, "textelem")))
print(TargetElem.text)
I do not think that you actually need a loop, since we are passing textelem id of div and all the span tags are inside the div, so .text should work.

Selenium Can't Find Element Returning None or []

im having trouble accessing element, here is my code:
driver.get(url)
desc = driver.find_elements_by_xpath('//p[#class="somethingcss xxx"]')
and im trying to use another method like this
desc = driver.find_elements_by_class_name('somethingcss xxx')
the element i try to find like this
<div data-testid="descContainer">
<div class="abc1123">
<h2 class="xxx">The Description<span data-tid="prodTitle">The Description</span></h2>
<p data-id="paragraphxx" class="somethingcss xxx">sometext here
<br>text
<br>
<br>text
<br> and several text with
<br> tag below
</p>
</div>
<!--and another div tag below-->
i want to extract tag p inside div class="abc1123", but it doesn't return any result, only return [] when i try to get_attribute or extract it to text.
When i try extract another element using this method with another class, it works perfectly.
Does anyone know why I can't access these elements?
Try the following css selector to locate p tag.
print(driver.find_element_by_css_selector("p[data-id^='paragraph'][class^='somethingcss']").text)
OR Use get_attribute("textContent")
print(driver.find_element_by_css_selector("p[data-id^='paragraph'][class^='somethingcss']").get_attribute("textContent"))

How to click on Web check box using Excel VBA?

How do I check the table checkbox?
I tried clicking.
ie.Document.getElementsByClassName("x-grid3-hd-checker").Checked = True
<div class="x-grid3-hd-inner x-grid3-hd-checker x-grid3-hd-checker-on" unselectable="on" style="">
<a class="x-grid3-hd-btn" href="#"></a>
<div class="x-grid3-hd-checker"> </div>
<img class="x-grid3-sort-icon" src="/javascript/extjs/resources/images/default/s.gif">
</div>
I can't see a checkbox in the HTML code. But you use getElementsByClassName() in a wrong way for your case. getElementsByClassName() generates a node collection. If you need a specific node, you must get it by it's index in the node collection. First element has index 0.
Please note that the div tag with the CSS class class="x-grid3-hd-inner x-grid3-hd-checker x-grid3-hd-checker-on " is also included in the Node Collection, because a part of the class identifier is identical to "x-grid3-hd-checker ". [Edit: I'm not realy sure if the part must maybe stand at the begin of the identifier]
If you want to check this:
<div class="x-grid3-hd-checker"> </div>
Your code needs the second index of the node collection:
ie.Document.getElementsByClassName("x-grid3-hd-checker")(1).Checked = True
But if there are more tags with the class name "x-grid3-hd-checker" the above line don't work. I can't say anymore until you don't post more HTML and VBA code. The best would be a link to the site.

Check for value of selector by find_element_by_xpath

I want to get the value of the "tabindex" selector of this HTML code:
<input name="ctl00$ctl00$placeContent$placeTopContent$filter$textAccount" type="text" value="110111102" id="ctl00_ctl00_placeContent_placeTopContent_filter_textAccount" style="width: 130px;" data-kpxc-id="ctl00_ctl00_placeContent_placeTopContent_filter_textAccount" tabindex="-1">
I tried with it with the following code, but I only get the value of "value". How can I correctly check for it?
driver.find_element_by_xpath('//*['#id="ctl00_ctl00_placeContent_placeTopContent_filter_textAccount" and #tabindex]').get_attribute('value')
Use attribute tabindex and following code.
driver.find_element_by_xpath('//input[#id="ctl00_ctl00_placeContent_placeTopContent_filter_textAccount"]').get_attribute('tabindex')
You can also use css selector.
driver.find_element_by_css_selector('#ctl00_ctl00_placeContent_placeTopContent_filter_textAccount').get_attribute('tabindex')

Resources