0

im just starting to learn python for a week now and want to us selenium to click on a div container containing specific text from two different elements.

    <div data-v-38f540f9="" class="order"><h6 data-v-58641c02="" data-v-38f540f9="" class="heading no-wrap" style="--margin-top:0; --margin-bottom:0; --color:rgb(39, 39, 39);">Dagdienst company</h6> <p data-v-49b0fbe1="" data-v-38f540f9="" class="paragraph body no-wrap" style="--color:rgb(39, 39, 39); --margin:0;">Nov 23</p> <div data-v-6e6d111c="" data-v-38f540f9="" class="horizontal work-location-time" style="--margin-left:0; --margin-right:0; --margin-bottom:0; --v-align:baseline; --justify:flex-start; --flex-wrap:nowrap;"><!----> <!----> <span data-v-38f540f9="" aria-label="icon" class="icon medium time" data-v-6e6d111c="" style="--background-color:transparent; --border-radius:0;"><svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" viewBox="0 0 56 56">
        <g fill="rgb(68, 179, 230)">
            <path d="M29.71 19v9.65l8.415 4.89-1.395 2.335-9.855-5.86V19h2.835zM28 42.625c2.627 0 5.08-.671 7.356-2.014a14.18 14.18 0 0 0 5.255-5.255A14.234 14.234 0 0 0 42.625 28c0-2.627-.671-5.08-2.014-7.356a14.18 14.18 0 0 0-5.255-5.255A14.234 14.234 0 0 0 28 13.375c-2.627 0-5.08.671-7.356 2.014a14.18 14.18 0 0 0-5.255 5.255A14.234 14.234 0 0 0 13.375 28c0 2.627.671 5.08 2.014 7.356a14.18 14.18 0 0 0 5.255 5.255A14.234 14.234 0 0 0 28 42.625zM28 10c3.288 0 6.332.822 9.13 2.466a17.06 17.06 0 0 1 6.404 6.404C45.178 21.668 46 24.712 46 28c0 3.288-.822 6.332-2.466 9.13a17.06 17.06 0 0 1-6.404 6.404C34.332 45.178 31.288 46 28 46c-3.288 0-6.332-.822-9.13-2.466-2.711-1.587-4.846-3.736-6.404-6.447C10.822 34.288 10 31.26 10 28s.822-6.288 2.466-9.087a17.715 17.715 0 0 1 6.447-6.447C21.712 10.822 24.74 10 28 10z"></path>
        </g>
    </svg></span> <p data-v-49b0fbe1="" data-v-38f540f9="" class="paragraph body no-wrap ellipsis" data-v-6e6d111c="" style="--color:rgb(68, 179, 230); --margin:0;">09:30 | MediaCenter - Rooster</p> <p data-v-49b0fbe1="" data-v-38f540f9="" class="paragraph body no-wrap ellipsis" data-v-6e6d111c="" style="--color:rgb(68, 179, 230); --margin:0;">&nbsp;| test.</p></div></div>

Now i want to click on this div only if it contains the text "dienst" in <h6> and "Nov 23" in <p>

i works if i only looking for text "Nov 23" with the code below but how can i add an extra check and only .click if text inside the div also contains text "dienst" in <h6> ?

today = ("Nov 23")

try:
    order = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//p[contains(text(),'{}')]".format(today))))
    print("workorder is found with text", '"' + (order.text) + '"')
    order.click()
except TimeoutException: 
    print("no workorder is found with text", '"' + (order.text) + '"')

1
  • Try to add the code to find the h6 after the one to find the p, then make the click action, it will only click if both lines don't raise error, or get the element and compare if it's the same as the one you want Commented Nov 23, 2021 at 20:42

1 Answer 1

1

Try to add the code to find the h6 after the one to find the p, then make the click action, it will only click if both lines don't raise error, or get the element and compare if it's the same as the one you want.

Try this:

today = ("Nov 23")

try:
  order = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//p[contains(text(),'{}')]".format(today))))
  print("workorder is found with text", '"' + (order.text) + '"')
  
  # I don't know if search by tag name in your case is the
  # best scenario, but you need to adjust if don't
  h6 = WebDriverWait(driver, 3).until(EC.presence_of_element_located(By.TAG_NAME, "h6")))
  if h6.text == 'dienst':
    order.click()
except TimeoutException: 
  print("no workorder is found with text", '"' + (order.text) + '"')

I think that can help you, if not make some adjusts

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this i think this will only work if there is one p an h6 element but it can be more in the same page. The code you suggest it search for individual elements right? It has to be in the same chain in other words it must only be a match if it find the elements together in one div with that specific text. To make it easier to understand here a cleaner html code with only the essential text and elements <div><h6>Dagdienst company</h6><p>Nov 23</p></div> i can imagine it must be something like this //div/h6[contains(text(), 'dienst']/p[contains(text(), '{}')]".format(today)
Yes, I didn't know the entire page, so I suggested something fixed that looks for only one h6 on the page, but your idea probably will work, if it's only possible to find only one element div with h6 named dienst.
My own command suggestion above doesnt work, <h6> and <p> are childs of <div> my command should work if <p> was a sub element of <h6>. I have to find out how to find a <div> element containing two childs <h6> and <p> containing text <div> <h6>Dagdienst company</h6> <p>Nov 23</p> </div>
I never really used the WebDriverWait before, so my suggestion will not use it. When you search elements, you can stack then, like: div_order = driver.find_elements(By.CLASS_NAME, "order"), that should give you the list of div that have the class "order", then you search for a h6 tag in the div, probably inside a loop: h6 = div_order[index_you_want].find_elements(By.TAG_NAME, "h6") that will give you the h6 tag you want, than you repeat the process to the p tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.