简体   繁体   中英

click on div contains specific text with selenium python

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM