python selenium find text of child of previous parent - python-3.x

I have the following document:
<div class="background-section ">
<h3 class='t-16'> Company1 </h3>
<p class="title t-14"> JobType </p>
<div class="background-section ">
<h3 class='t-16'> Company2 </h3>
<p class="title t-14"> JobType2 </p>
by trying the following:
driver.find_element_by_xpath("//p[contains(text(), 'JobType')]")
I still need to extract "Company1" text too but I could not relate
I want the output to find "Company1" related to JobType
Any idea?

You can use xpath here as it is used to identify and navigate nodes in HTML.
Try below code and let me know if it works for you :-
Mytext = driver.find_element_by_xpath("//p[contains(text(),'JobType')]/parent::div/h3").text
The output will be -

Related

access test data after test area in cypress

I have an html code with contains
<div class="form-card">
<h2 class="fs-title" test-data="area_1">
About DigCompEdu
</h2>
<p></p>
<label for="academic_teaching" class="question" test-data="question_1">
<b>1- </b>
</label>
<small id="academic_teachingHelp" class="form-text text-muted">
</small>
<div class="form-check question-form-check ">
<div class="row">
<div class="col-1">
<input class="radio" type="radio" value="0" id="3" name="2" test-data="answer_1" />
</div>
</div>
So, I have h1 with testdata name after that i have a form, this form contains question with multin check radio .. i want to access this check , not by just call ([test-data="answer_2"])
because i have another tips and don't want to check them as this one ,
i did smth like this but it's not working :
cy.get('[test-data="area_1"]~[test-data="answer_2"]').check({force: true})
Any one have another idea ?
It might be the ~ between the selectors, I haven't seen that before.
Does it work with a space between instead?
cy.get('[test-data="area_1"] [test-data="answer_2"]').check({force: true})
If the heading is followed by the form like this
<h2 class="fs-title" test-data="area_1">
About DigCompEdu
</h2>
<div class="form-check question-form-check ">
...
then you would query like this
cy.get('[test-data="area_1"]')
.next('.form-check') // next element on page
.find('[test-data="answer_2"]') // this looks inside '.form-check'
.check({force: true})

How to find if there is an element in a div?

I have a list of posts and some of them has reactions, some not. I want to extract the number of reactions of each posts and the problem is that those who doesn't have, there is no HTML code.
I tried to debug using print.
print(len(driver.find_elements_by_xpath("//div[#class='occludable-update ember-view']")))
-----
Output: 4 - This is good, because I have 4 divs
print(len(driver.find_elements_by_xpath("//span[#class='v-align-middle social-details-social-counts__reactions-count']"))
----
Output:2 - This is good, because I have 2 posts that got reactions
When I tried to find if a posts got reactions,
print(len(driver.find_elements_by_xpath("//div[#class='occludable-update ember-view']")[1].find_elements_by_xpath("//span[#class='v-align-middle social-details-social-counts__reactions-count']")))
----
Output: 2 - Why? Because the second div is empty...
HTML CODE
<div class="occludable-update ember-view">
<ul class="social-details-social-counts ">
<li class="social-details-social-counts__reactions social-details-social-counts__item ">
<button class="social-details-social-counts__count-valuet " aria-label="1 Reaction’ post" type="button">
<img class="reactions-icon social-detail">
<span aria-hidden="true" class="v-align-middle social-details-social-counts__reactions-count">3</span>
</button>
</li></ul>
</div>
<div class="occludable-update ember-view"> this div has no reaction </div>
<div class="occludable-update ember-view"> this div has no reaction </div>
<div class="occludable-update ember-view">
<ul class="social-details-social-counts ">
<li class="social-details-social-counts__reactions social-details-social-counts__item ">
<button class="social-details-social-counts__count-valuet " aria-label="1 Reaction’ post" type="button">
<img class="reactions-icon social-detail">
<span aria-hidden="true" class="v-align-middle social-details-social-counts__reactions-count">3</span>
</button>
</li></ul>
</div>
to locate the continuation element, use .
try below :
driver.find_elements_by_xpath("//div[#class='occludable-update ember-view']")[1].find_elements_by_xpath(".//descendant::span[#class='v-align-middle social-details-social-counts__reactions-count']")
I found the problem. After better documenting myself, I found out that for sub-elements is used .// instead of //
print(len(driver.find_elements_by_xpath("//div[#class='occludable-update ember-view']")[1].find_elements_by_xpath(".//span[#class='v-align-middle social-details-social-counts__reactions-count']")))

Divs and H tags inside of A tags

In Kentico, I am trying to put an image and an h3 tag inside an a tag in an Editable Text web part, but Kentico rewrites the code and splits the a tags, making 2 a tags, one around the image and one around the h3. How would I get this to stop?
Here is what I am trying to write:
<div class="brick">
<a href="/virtualvalley/studentresources.aspx">
<img alt="" class="img-responsive" src="/Virtual-Valley/document-library-(1)/images/default.jpg" />
<h3>Student Resources</h3>
</a>
</div>
And here is what Kentico rewrites it as:
<div class="brick">
<a href="/virtualvalley/studentresources.aspx">
<img alt="" class="img-responsive" src="/Virtual-Valley/document-library-(1)/images/default.jpg" />
</a>
<h3>
Student Resources
</h3>
</div>

unable to select react id | watir

Hi Fairly new to watir and came across this problem. How can I select the button in the following snippet of code
<div id="side bar" class="sidebar">
<div class="inner active" data-reactid=".1">
<a class="back side bar" data-reactid=".1.0" href="#overview">
<h2 data-reactid=".1.1">
<div class="price clearfix" data-reactid=".1.2">
<div class="values type-current-value" data-reactid=".1.3">
<div class="values date-current-value" data-reactid=".1.4">
<div class="values duration-current-value" data-reactid=".1.5">
<div class="values passengers-current-value" data-reactid=".1.6">
<div class="values yacht-current-value" data-reactid=".1.7">
<div class="values flight-current-value" data-reactid=".1.8">
<div class="share-quote" data-reactid=".1.9">
<a class="share-quote cta-button cta-button-blue = share-quote-processed" data-reactid=".1.9.0" data-modal-url="/share-quote" href="#">Share this quote</a>
</div>
I am trying the following which produces a no method error
b.links(:xpath => '//div[#class="share-quote"]/a').to_a.click
The code is trying to click an array of links, rather than an individual link. That is why you get an undefined method error.
You need to click a specific link within the collection. For example:
# Click the first link
b.links(:xpath => '//div[#class="share-quote"]/a').first.click
# Click the last link
b.links(:xpath => '//div[#class="share-quote"]/a').first.click
# Click the nth link
b.links(:xpath => '//div[#class="share-quote"]/a')[n].click
Assuming there is only one of these links on the page, it would be more Watir-like to do:
b.div(class: 'share-quote').link.click

How to show a message if there are no products inside a category with exp:resso store plugin?

I'm using the latest version of EE2 and a plugin called Exp:resso store.
I have products assigned to a category and for the most part all of this is working fine. Below is my code:
<div class="col-md-7">
{exp:channel:categories channel="products" style="linear"}
<section class="section accordion repeater">
<h3>
{category_name}
<div class="icon">
<img src="/assets/local/img/plus-icon.jpg" alt="">
</div>
</h3>
<div class="accordion-content">
{exp:store:search orderby="title" sort="asc" category="{category_id}"}
{exp:store:product entry_id="{entry_id}"}
<p class="accordion-download">
{title} - {price}
<span><img src="/assets/local/img/add-to-cart.jpg" alt="">Add to cart</span>
</p>
{/exp:store:product}
{/exp:store:search}
</div>
</section>
{/exp:channel:categories}
</div>
I'm trying to find a way to show a No products exist message if the category doesn't have anything inside of it. I've tried using {count}, {total_results} & {total_rows} to check if there aren't any products. Problem is everything I try is obviously wrong because nothing gets output :/
Thanks in advance
The store search tag is a wrapper for the channel entries tag pair so you would need to use the {if no_results} tag pair.
<div class="col-md-7">
{exp:channel:categories channel="products" style="linear"}
<section class="section accordion repeater">
<h3>
{category_name}
<div class="icon">
<img src="/assets/local/img/plus-icon.jpg" alt="">
</div>
</h3>
<div class="accordion-content">
{exp:store:search orderby="title" sort="asc" category="{category_id}"}
{exp:store:product entry_id="{entry_id}"}
<p class="accordion-download">
{title} - {price}
<span><img src="/assets/local/img/add-to-cart.jpg" alt="">Add to cart</span>
</p>
{/exp:store:product}
{if no_results}
There are no products
{/if}
{/exp:store:search}
</div>
</section>
{/exp:channel:categories}
</div>
Should also be mentioned if you are not creating a form for the to add the products to the cart you could use the {store_field_short_name:price} variable to reduce the number of queries on your page. Most store things such as sku, weight, measurements can all be access by using the field short name followed by :variable

Resources