How to get xpath using object id? - object

I have HTML code for a input box with following expression:
<div id="divId">
<object id="myId" align="middle" type="application/myId">
<param value="myValue">
</object>
</div>
If i use div id for Xpath, then it points to div bar, but i need xpath for 'Object' only.
How to get xpath for the above expression.

Without further detail I can only suggest this XPath :
//Object[#id='myId']

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 select unique attribute of div in cypress

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()

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"))

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')

How can i click the third href link?

<ul id='pairSublinksLevel1' class='arial_14 bold newBigTabs'>...<ul>
<ul id='pairSublinksLevel2' class='arial_12 newBigTabs'>
<li>...</li>
<li>...</li>
<li>
<a href='/equities/...'> last data </a> #<-- HERE
</li>
<li>...</li>
Question is how can i get click third li tag ??
In my code
xpath = "//ul[#id='pairSublinksLevel2']"
element = driver.find_element_by_xpath(xpath)
actions = element.find_element_by_css_selector('a').click()
code works partially. but i want to click third li tag.
The code keeps clicking on the second tag.
Try
driver.find_element_by_xpath("//ul[#id='pairSublinksLevel2']/li[3]/a").click()
EDIT:
Thanks #DebanjanB for suggestion:
When you get the element with xpath //ul[#id='pairSublinksLevel2'] and search for a tag in its child elements, then it will return the first match(In your case, it could be inside second li tag). So you can use indexing as given above to get the specific numbered match. Please note that such indexing starts from 1 not 0.
As per the HTML you have shared you can use either of the following solutions:
Using link_text:
driver.find_element_by_link_text("last data").click()
Using partial_link_text:
driver.find_element_by_partial_link_text("last data").click()
Using css_selector:
driver.find_element_by_css_selector("ul.newBigTabs#pairSublinksLevel2 a[href*='equities']").click()
Using xpath:
driver.find_element_by_xpath("//ul[#class='arial_12 newBigTabs' and #id='pairSublinksLevel2']//a[contains(#href,'equities') and contains(.,'last data')]").click()
Reference: Official locator strategies for the webdriver

Resources