简体   繁体   中英

How to search for text in a webpage within a specific area using Selenium in Python

So I've been trying to search the webpage for text, but just in a specific region (the red circled area in the pictures above^). The region I want to search in is the comment by the first user, in this case @chris01733 . I've been using Xpaths to try locate the text, but it finds text from the entire webpage, and not specifically the region I'm looking in. I want to find a way to narrow down my search to that region.

Code so far:

if driver.find_elements_by_xpath("//*[contains(text(), 'Hi there, thank you for contacting the giffgaff Community.')]"):
    time.sleep(15)
    print("It's here")
else:
    print("Nothing here yet 1")
    if driver.find_elements_by_xpath("//*[contains(text(), '500 point')]"):
        login_to_reply = driver.find_element_by_xpath("//body/div[@id='app']/main/div[@id='content']/div/div/div/div/div/div/article/header[1]")
        login_to_reply.click()                         

In this specific case, I'd like to search up the term "500 point", however, as shown in the third image, chrome returns 2 values when you search for the keyword on the webpage. I'm not sure what xpath to use when trying to find the keyword in that area, so I basically need help with how to narrow down my search to just that region. Help very much appreciated, thanks for reading.

Edit: So these pages have an option to pin answers on the side (from previous posts), these answers also contain keywords which can cause confusion in the program when trying to search the page for them. Again, the region I circled red is where I want to search (different post in the 4th image provided).

So I've been trying to search the webpage for text, but just in a specific region (the red circled area in the pictures above^). The region I want to search in is the comment by the first user, in this case @chris01733 . I've been using Xpaths to try locate the text, but it finds text from the entire webpage, and not specifically the region I'm looking in. I want to find a way to narrow down my search to that region.

Code so far:

if driver.find_elements_by_xpath("//*[contains(text(), 'Hi there, thank you for contacting the giffgaff Community.')]"):
    time.sleep(15)
    print("It's here")
else:
    print("Nothing here yet 1")
    if driver.find_elements_by_xpath("//*[contains(text(), '500 point')]"):
        login_to_reply = driver.find_element_by_xpath("//body/div[@id='app']/main/div[@id='content']/div/div/div/div/div/div/article/header[1]")
        login_to_reply.click()                         

In this specific case, I'd like to search up the term "500 point", however, as shown in the third image, chrome returns 2 values when you search for the keyword on the webpage. I'm not sure what xpath to use when trying to find the keyword in that area, so I basically need help with how to narrow down my search to just that region. Help very much appreciated, thanks for reading.

Edit: So these pages have an option to pin answers on the side (from previous posts), these answers also contain keywords which can cause confusion in the program when trying to search the page for them. Again, the region I circled red is where I want to search (different post in the 4th image provided).

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